多种搜索模式(等于加不等于)

时间:2014-10-14 08:29:34

标签: jquery

在JQuery中我试图执行以下

  1. 查找页面上的每个链接
  2. HREF属性以'#'
  3. 开头
  4. 并且ID值不是“mobileMenu'
  5. 这非常有效:

    $("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 () {
    
    }
    

    我做错了什么?

2 个答案:

答案 0 :(得分:2)

您不应该使用多个选择器,因为您想要一个AND条件

$('a[href^="#"]:not(#mobileMenu)').each(...)

答案 1 :(得分:1)

尝试此操作:使用.not()从所选元素列表中排除元素。

$("a[href^='#']").not('#mobileMenu').each(function () {

}