我是 jQuery 新手,希望有人能帮助我。
我目前使用下面的代码片段,到目前为止它的效果符合预期。
但是,我现在需要更改此设置,以便它可以用于动态添加的元素,并且我在此处面临 .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...
});
关于javascript - 如何使用 .not 更改选择器以应用于 dom 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371507/