Parameters of the jquery event handling handler function
In the following function, we often use in the process of jquery:
- $ ("#foo"). Bind ("click", Function () {
). text ());
The handler handler for click is typically function () {}, and does not pass arguments. So, can this function receive parameters, what object is built in, what return value, and how to get the object bound by the event?
1, the first parameter of the event object, in the function functions, the actual first parameter is an event
Reference
The handler callback function can also take parameters. When the function is called, the event object would be passed to the first parameter.
2. Event parameter Event.data
Pass the arguments to handler that you need to pass in the event handler function
Reference
var message = "spoon!";
$ ("#foo"). Bind ("click", {
Msg:message
}, function (event) {
alert (event.data.msg);
});
Message = "Not in the face!";
$ ("#bar"). Bind ("click", {
Msg:message
}, function (event) {
alert (event.data.msg);
});
3. Get the event-bound DOM element,
The This of handler points to the element of the current event binding, and in most cases we need to get the DOM element to take the value
or change the display state, this is the DOM element of the handler function.
If we do not know, we have been using Event.target to fetch the element of the event, the actual error is often taken, because the trigger
The event.target of the event may be an intrinsic element of the actual binding element, such as
JS Code
- <div><p> Click </p></div>
As shown above, the binding event to the Div, the actual p tag can also trigger the event, when the Event.target object is not originally bound
The element of the event.
Accidentally found this pointer to the binding DOM primitive, had to admire John, think very well, functional to the benefit.
4. Event propagation and return values
As long as false is returned, it represents. Preventdefault () and. Stoppropagation (); Stop event propagation.
There is no need to manually call these two more functions.
Reference
Returning false from a handler are equivalent to calling both. Preventdefault () and. Stoppropagation () on the event object.
Reference
The creator of the JavaScript field, JQuery, Pro JavaScript and JavaScript secrets, Dean John Resig,john of the School of Computer science at Khan Academy Resig was a sophomore when he developed jquery. John Resig's dialogue now has a large number of JavaScript frameworks on the market, claiming to be an unbeatable framework on their respective official networks, and what are the real advantages and characteristics of jquery compared to them, and why do we choose to use jquery? John Resig that the biggest advantage of jquery is that it focuses entirely on the DOM, finding objects and then controlling them, and the size of the file is very small when it comes to providing these features. For example, now the browser provides a variety of hardware acceleration, as the browser becomes more and more good, JS performance is getting better. A question was asked: What is the development team of jquery? John Resig Revealed: There are more than 20 people. JQuery Core's code submitter has 3 people. We work primarily with git and GitHub. There are students in the field also raised an interesting question, John Resig will not write code to work selflessly, will listen to rock music. Resig answer, will work late, prefer to listen to jazz. In addition, many participants also communicated with John Resig on many of the practices of the jquery development process (Zhangjiang)
John Resig
Original Address http://techfoxbbs.com/blog-1-4.html
Parameters of the jquery event handling handler function