繁体   English   中英

jQuery如何禁用特定选择器的功能?

[英]Jquery how to disable functions for specific selectors?

这是我的选项卡式菜单的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');
    });

$('li.selected #submenu');

问题是,当悬停在子菜单或li元素上时,#li.selected #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');
});

但是,如果使用else语句则非常难看。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM