Jquery如何禁用特定选择器的函数

Jquery how to disable functions for specific selectors?

本文关键字:选择器 函数 何禁用 Jquery      更新时间:2023-09-26

这是我的Jquery标签菜单:

var current = $('li.selected');
$('li.selected #submenu').css("display", "block").addClass('lihovered');
current.addClass('lihovered');
    $("ul#ulmenu li").hover(function() { //Hover over event on list item
        $(this).find("#submenu").show(); //Show the subnav
        $(this).addClass('lihovered');
        current.removeClass('lihovered');
    } , function() { //on hover out...
        $(this).find("#submenu").hide(); //Hide the subnav
        $(this).removeClass('lihovered');
        current.addClass('lihovered');
    });
    $("ul#submenu").hover(function() { //Hover over event on list item
        $(this).show(); //Show the subnav
        $(this).parent("li").addClass('lihovered');
        $(this).parent("li").find('a').addClass('lihovereda');
    } , function() { //on hover out...
        $(this).find("#submenu").hide(); //Hide the subnav
        $(this).parent("li").removeClass('lihovered');
        $(this).parent("li").find('a').removeClass('lihovereda');
        current.addClass('lihovered');
    });

问题是,当悬停悬停时,被选中的袋子被移除。因此,我想禁用此功能:

        $("ul#ulmenu li").hover(function() { //Hover over event on list item
            $(this).find("#submenu").show(); //Show the subnav
            $(this).addClass('lihovered');
            current.removeClass('lihovered');
        } , function() { //on hover out...
            $(this).find("#submenu").hide(); //Hide the subnav
            $(this).removeClass('lihovered');
            current.addClass('lihovered');
        });

对于$('li.selected');

我也有这个问题,当悬停悬停在菜单中的另一个li元素被选中的子菜单被隐藏。因此,我想禁用此功能:

    $("ul#submenu").hover(function() { //Hover over event on list item
        $(this).show(); //Show the subnav
        $(this).parent("li").addClass('lihovered');
        $(this).parent("li").find('a').addClass('lihovereda');
    } , function() { //on hover out...
        $(this).find("#submenu").hide(); //Hide the subnav
        $(this).parent("li").removeClass('lihovered');
        $(this).parent("li").find('a').removeClass('lihovereda');
        current.addClass('lihovered');
    });

for $('li.selected #submenu');

问题是#li。当鼠标悬停在子菜单或li元素上时,选中的#submenu被隐藏。这是不应该的。

更新,我已经尝试过这个,但它是(不)工作:

$("ul#ulmenu li").hover(function() { //Hover over event on list item
    if($(this).hasClass('selected')) {
    } else {
    $(this).find("#submenu").show(); //Show the subnav
    $(this).addClass('lihovered');
    current.removeClass('lihovered');
    }
} , function() { //on hover out...
    $(this).find("#submenu").hide(); //Hide the subnav
    $(this).removeClass('lihovered');
    current.addClass('lihovered');
});

但是if else语句非常难看。

使用not()方法:http://api.jquery.com/not/

$("ul#ulmenu li").not('.selected').hover(function() {

and:not选择器http://api.jquery.com/not-selector/

$('li:not(.selected) ul#submenu').hover(function() {