Click (), Bind (), Live () are all methods used when executing an event
1.click () Click event Method:
$ ("a"). Click (function () { alert ("Hello");});
2. In bind (), jquery all JavaScript event objects, such as focus, mouseover, and resize, can be passed in as type parameters.
var message = "Left"; $ ("a"). Bind ("click", Function () { alert (message); return false;});
var message = "Left"; $ ("a"). Bind ("click", {msg:message}, function (e) { alert (e.data.msg); return false;}); var message = "Right"; $ ("a"). Bind ("ContextMenu", {msg:message}, function (e) { alert (e.data.msg); return false;});
The bind () method is used to attach a handler to each matching element's event and return the jquery object.
. Bind (eventtype[, Evnetdata], Handler (EventObject))
Where the parameter eventtype is a string that contains one or more JavaScript event types, such as the name of a click,submit or custom event, with spaces separating each type when specifying multiple event types; EventData is the map type. Gives the data to be passed to the event handler, handler specifies the function to execute when the event is triggered, and EventObject represents the event object.
The. Bind () method attaches the event handler handler to the EventType event of each element in the matching element collection, and can also pass data to the event handler if needed.
3.live () Appends an event handler function to all matching elements, even if the element is added later.
If the element is added after the call to bind (), the corresponding event cannot be executed. The use of the Live () method allows the element to be added at the back to perform the corresponding event, as follows:
$ ("Div.live"). Live ("Click", Function () { alert ("Success");});
This way, when we click the a tag of the class live, if this a tag is added behind, it can also output "success" as usual.
One disadvantage of the live () method is that it does not support chained notation:
$ ("#test"). Children ("a"). Live ("MouseOver", function () { alert ("Hello");}); The above notation does not output
Using delegate () can be written as:
$ ("#test"). Delegate ("A", "mouseover", function () { alert ("Hello");});
$ (' body '). Delegate (' A:not ([target= ' _blank "]) ', ' click ', Function (e) { if ($ (this). attr (' href ') = = Location.hash) { $ (window). Trigger (' Hashchange '); } });
Stop event Propagation
$ (' a '). bind (' click ', Function (e) { e.preventdefault ()
if (e.stoppropagation) { e.stoppropagation ();}
E.cancelbubble = true;
E is a bunch of arguments that are passed in after the event is triggered, indicating that the event object
Click () Bind () Live () delegate () Difference