在JQuery中我试图执行以下
这非常有效:
$("a[href^='#']").each(function () { // links where HREF starts with #
if ($(this).attr('id') != 'mobileMenu') { // id not equal to mobileMenu
$(this).click(function () {
// logic
})
}
});
但是当我尝试将它组合成一个更简洁的搜索时,它会失败(mobileMenu链接仍被拉入数组中):
$("a[href^='#'], a[id!='mobileMenu']").each(function () {
}
我做错了什么?
答案 0 :(得分:2)
您不应该使用多个选择器,因为您想要一个AND条件
$('a[href^="#"]:not(#mobileMenu)').each(...)
答案 1 :(得分:1)
尝试此操作:使用.not()
从所选元素列表中排除元素。
$("a[href^='#']").not('#mobileMenu').each(function () {
}