The correct way to cancel and bind hover events in jquery

Source: Internet
Author: User

In web design, we often use jquery to respond to mouse hover events, and mouseover and mouseout events have the same effect, but how do you use bind to bind the hover method? How do I unbind an event with unbind?
First, how to bind the hover event
Let's look at the following code, assuming we bind a click and hover event to the A tag:
$ (document). Ready (function () {$ (' a '). Bind ({hover:function (e) {//
Hover event handler alert ("Hover"); }, Click:function (e) {//click
Event handler alert ("click"); } }); });
When you click on the a tag, something strange happens, where the bound hover event is completely unresponsive, and the bound click event responds normally.

But if you change the wording, for example:
$ ("a"). Hover (function () {alert (' mouseover ');}, function () {
Alert (' mouseout '); })
This piece of code can run normally, can't bind hover bind?

In fact, you should use both MouseEnter and MouseLeave events instead, (which is also the. Hover () event used in the function)
So you can simply refer to it like this:
$ (document). Ready (function () {$ (' a '). Bind ({mouseenter:function (e) {//
Hover event handler alert ("MouseOver"); }, Mouseleave:function (e) {//
Hover event handler alert ("Mouseout"); }, Click:function (e) {//click
Event handler alert ("click"); } }); });
Because. Hover () is a jquery-defined event that is designed to make it easier for users to bind calls to MouseEnter and MouseLeave events, which is not a real event, so of course it cannot be called as an event argument in. bind ().
Second, how to cancel the hover event
As you all know, you can use the Unbind function to unbind an event, but only the event that is bound by bind, the hover event in jquery is quite special, and if the event is bound in this way, it cannot be canceled.
$ ("a"). Hover (function () {alert (' mouseover ');}, function () {
Alert (' mouseout '); })
The correct way to unbind the hover event:
$ (' a '). Unbind (' MouseEnter '). Unbind (' MouseLeave '); Iii. summary
In fact, these questions can be seen in the official jquery documentation, but very few people go to see, most of the online tutorials just explain how to use this method, to achieve the goal, and do not have a deep understanding of why this writing?

If you have any doubts, please leave a comment.


Original link: http://www.uedsc.com/jquery-bind-hover-event.html

The correct way to cancel and bind hover events in jquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.