当鼠标进入二者的区域时触发fun1,离开时触发fun2。你也许会想到使用下面的方式
Home > Web Front-end > JS Tutorial > Hover event solution for multiple elements in jQuery_jquery

Hover event solution for multiple elements in jQuery_jquery

WBOY
Release: 2016-05-16 16:44:47
Original
1650 people have browsed it
1. Introduction to requirements

jQuery’s hover event is only for a single HTML element, for example:
Copy code The code is as follows:

$('#login').hover(fun2, fun2);

The fun1 function is called when the mouse enters the #login element , the fun2 function is called when leaving. This API can meet most needs.

However, sometimes we want fun1 to be triggered when the mouse enters two or more elements, and fun2 to be triggered when the mouse leaves them, while moving the mouse between these elements does not trigger any events. For example, two HTML elements are next to each other, as shown below:

Hover event solution for multiple elements in jQuery_jquery

Fun1 is triggered when the mouse enters the area between the two, and fun2 is triggered when the mouse leaves. You may think of using the following method
Copy code The code is as follows:

$('# trigger, #drop'),hover(fun1, fun2);

This method does not meet our needs, because fun2 and fun1 will be triggered when entering #drop from #trigger. To solve this problem, a relatively simple way is to change the HTML structure. The implementation is as follows:
Copy code The code is as follows:



< ;/div>


$('#container').hover(fun1, fun2);

This way by binding on the parent element hover event to achieve this function.

2. Example study

The picture below is a simplified diagram of a common drop-down menu. The HTML structure is as follows:

Hover event solution for multiple elements in jQuery_jquery
Copy code The code is as follows:

ul id="#nav">
  • < ;/li>


  • Drop-down menu

    • Dropdown item 1

    • Dropdown item 2

    • Dropdown item 3






  • The implemented JavaScript program is also very simple
    Copy code The code is as follows:

    $('#droplist').hover(function(){
    $( this).find('ul').show();
    }, function(){
    $(this).find('ul').hide();
    });

    This implementation method has clear logic, but it results in too many nested levels of HTML and a lot of inconvenience when writing CSS. For example:
    Copy code The code is as follows:

    #nav li { font-size:14px; }

    We want this CSS to set a 14-pixel font for the first-layer li element, but it also affects the second-layer element, so we have to use the following statement to rewrite it
    Copy code The code is as follows:

    #nav li li { font-size:12px; }

    3. Solution

    Change HTML structure
    Copy code The code is as follows:




    • Drop-down menu




    • Dropdown item 1

    • Dropdown item 2

    • Dropdown item 3



    Introduce JS files in sequence
    Copy the code The code is as follows:


    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template