4

我正在尝试使用该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

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

4

1 回答 1

11

根据规范,将您要定位的值转换为字符串将起作用:

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

您当前版本的问题是,除非您使用引号,否则值必须符合对identifiers的限制,而它们没有。

于 2012-03-18T23:45:53.920 回答