There are three ways to bind events in jquery, and here are three ways to bind
(1) Element.click (function () {});
(2) Element.bind ("Click", Function () {});
(3) element.live ("Click", Function () {});
Note that the first way to bind is similar to the previous one, (Here you save a keyword on)
Note that this is written in the API.
$ ("P"). On ("click", Function () {
Alert (This). text ());
});
The point is to say that the latter two, in fact, they are also bound events appear to be similar to the first kind, but there is a great but not
The live method is actually a variant of the Bind method, and its basic function is the same as the function of the Bind method, which binds an event to an element.
But the Bind method can only give the current element binding event, for the subsequent use of JS and other methods of the new generation of invalid elements, and the live method just make up for the bind method of this flaw, it can be generated after the elements can also bind the corresponding
The event.
$ (' span '). Live ("click", Fun)
function Fun () {
Alert ("Hello")
}
var $v =$ ("<span>hello world</span>")
$ (' body '). Append ($v)
Then why is there live we still need bind? Note that bind is not replaceable.
The difference:
1: Test Event bubbling
2:live () does not fully support the elements found through the DOM traversal method. Instead, it should always be used directly behind a selector. Live ()
The 3:bind method can bind to any JavaScript event, while the live method supports not many methods at jQuery1.3 time
Typical events
1:hover a simulated hover event
Note: Mouse at this time there are two situations, enter and go out
$ (' span '). Hover (function () {
Alert (' a ')
},function () {
Alert (' B ')
})
So how do you use bind or leave?
var $v =$ ("<span>click me</span>")
$ (' body '). Append ($v);
$ (' span '). Live ({
Mouseenter:enter,
Mouseleave:leave
})
function Enter () {
Alert ("Enter")
}
function Leave () {
Alert ("Leave")
}
2:toggle is used to bind two or more event handler functions in response to the selected element's rotation click event.
$v. Toggle (function () {
Alert ("Hello")
},function () {
Alert ("World")
})
jquery Event Bindings (2)