【问题标题】:Using colons in querySelectorAll()在 querySelectorAll() 中使用冒号
【发布时间】:2012-04-03 12:49:15
【问题描述】:

我正在尝试使用querySelectorAll() 方法来抓取网页中的链接,但我想忽略以“javascript:”开头的链接或使用其他协议,如“itpc://”

有没有办法将这些包含在“not()”伪选择器中?

document.querySelectorAll("a:not([href^=javascript]):not([href^=itpc])"); //works
document.querySelectorAll("a:not([href^=javascript:]):not([href^=itpc://])"); //doesn't work

即使第一种方法在当前页面上运行良好,但不能保证它在我将使用它的每个页面上都适用,所以我真的希望能够检测到那个冒号。

【问题讨论】:

    标签: javascript css css-selectors selectors-api


    【解决方案1】:

    基于the spec,将您要定位的值转换为strings 将起作用:

    document.querySelectorAll("a:not([href^='javascript:']):not([href^='itpc://'])");
    

    您当前版本的问题是,除非您使用引号,否则值必须符合identifiers 上的限制,而他们不这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-31
      • 2012-05-10
      • 2011-05-02
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多