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.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn