Before introducing these two methods first, we commonly use the click() method
$("a").click(function() {
alert("hello");
});
click( ) method is a simple method of bind() method. In bind(),
all jQuery JavaScript event objects, such as focus, mouseover, and resize,
can be passed in as type parameters.
Parameters: type,[data],function(eventObject)
For example:
$("p").bind("click",function(){
alert("hello");
})
You can also pass parameters
var message = "how are you!";
$("p").bind("click",{msg:message},function(e){
alert(e.data.msg);
} )
live() attaches an event handler to all matching elements,
even if the element is added later. As follows:
Click me |
$(".mytd").bind("click",function(){
alert("hello");
})
Click me and it will pop up hello
Add a new element at this time
$ (".mytr").after("
added after |
");
This When using bind to click "added after", it will not be executed
Use the live() method instead
$(".mytd").live("click",function(){
alert("hello");
})
The
.live() method can be effective on an element that has not been added to the DOM due to the use of event delegation:
Event handlers bound to ancestor elements can respond to events triggered on descendants. .
The event handler passed to .live() will not be bound to the element, but will be treated as a special event handler and bound to the root node of the DOM tree.