繁体   English   中英

如何使用.not更改选择器而不应用于dom元素

[英]How to change selector with .not to apply to dom elements

我是jQuery的新手,希望有人可以帮助我。

我目前使用下面的代码段,到目前为止可以正常使用。 但是, 我现在需要更改此设置,以便可以将其用于动态添加的元素,并且.not.not这里的一些问题。

我当前的代码:

$('.customSelect dd ul li a').not('.disabled, .selectMain').on('click', function(){
    var selectHtml = $(this).html(),
        selectVal = $(this).next('span.selectVal').text();
    $(this).closest('div.divSelect').attr('name', selectVal).find('dt a span.selectActive').html(selectHtml).end().find('dd ul').hide();
});

我知道我可以编写以下内容,但不确定如何将.not then应用于此内容:

$(document).on('click', '.customSelect dd ul li a', function(){
    // ...
});

非常感谢您,对于这个初学者的问题,
麦克风

您可以使用:not选择器:

$(document).on('click', '.customSelect dd ul li a:not(.disabled, .selectMain)', function() {
    // do something...
});

或者,您可以使用hasClass()方法:

$(document).on('click', '.customSelect dd ul li a', function() {
    if ($(this).hasClass('disabled') || $(this).hasClass('selectMain'))
        return;

    // do something...
});

暂无
暂无

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

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