【问题标题】:Struggling with jQuery conditional selectors挣扎于 jQuery 条件选择器
【发布时间】:2014-04-05 11:18:02
【问题描述】:

我已经想了好几个小时了。

我想选择所有以属性“http://localhost”开头的锚链接,并排除其中包含“wp-admin”的所有链接。

我可以选择所有以 'http://localhost' 开头的链接:

$internalLinks = $("a[href^='http://localhost']")

这是我迄今为止排除“wp-admin”链接的方法,但它不起作用:

$internalLinks = $("a:not[href*='wp-admin']a[href^='http://localhost']"),

我错过了什么?

【问题讨论】:

    标签: javascript jquery jquery-selectors


    【解决方案1】:

    你几乎得到它。您正在寻找的选择器是这样的:

    a[href^='http://localhost']:not([href*='wp-admin'])
    

    您犯的两个错误是 1) 重复标签名称 (a) 和 2) 忘记了 :not() 选择器的括号。

    【讨论】:

    • 啊!!!而已!我的眼睛流血了4个小时。太感谢了!我只是按照此处发布的内容进行操作:stackoverflow.com/questions/10687131/… 我找不到有关如何正确订购它的任何文档。随着时间的推移,有什么事情或者你只是知道吗?
    • @Kenny:顺序通常无关紧要,除了标签名称必须在前。
    【解决方案2】:

    我想选择所有以属性http:/localhost 开头的锚链接,并排除其中包含“wp-admin”的所有链接。

    鉴于您的病情相对复杂,我会考虑使用filter()

    $('a').filter(function() {
        // starts with 'http://localhost' and does not contain 'wp-admin'
        return this.href.indexOf('http://localhost') == 0 && 
           this.href.indexOf('wp-admin') == -1;
    });
    

    在一个表达式中完成所有操作并不意味着您可以避免评估页面上的所有锚点,因此您不妨让自己更轻松。

    【讨论】:

      【解决方案3】:

      试试这个

       $internalLinks = $("a[href^='http://localhost']").not("a[href*='wp-admin']");
      

      示例

      http://jsbin.com/burobako/1/edit

      【讨论】:

        【解决方案4】:

        使用类似的东西

        $("a:not([href^=wp-admin])");
        

        并查看此链接

        jQuery selector question. Select all nodes that do NOT START with (string)

        【讨论】:

          猜你喜欢
          • 2017-12-20
          • 2023-04-06
          • 2014-08-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-21
          • 2015-08-24
          • 2021-08-05
          相关资源
          最近更新 更多