modern-web-design-and-development

(Brent) #1
1 $('#table td').bind('click', {message: 'You left clicked a
TD'}, function(e) {

(^2) alert(e.data.message);
3 });
4 $('#table td').bind('contextmenu', {message: 'You right clicked
a TD'}, function(e) {
(^5) alert(e.data.message);
6 });
Events bound with bind() and with the alias methods (.mouseover(),
etc) are unbound with the unbind() method.


live()


This works almost exactly the same as bind() but with one crucial
difference: events are bound both to current and future elements — that is,
any elements that do not currently exist but which may be DOM-scripted
after the document is loaded.


Side note: DOM-scripting entails creating and manipulating elements in
JavaScript. Ever notice in your Facebook profile that when you “add another
employer” a field magically appears? That’s DOM-scripting, and while I
won’t get into it here, it looks broadly like this:


1 var newDiv = document.createElement('div');
2 newDiv.appendChild(document.createTextNode('hello, world!'));
3 $(newDiv).css({width: 100, height: 100, background: '#f90'});
4 document.body.appendChild(newDiv);
Free download pdf