在querySelectorAll()中使用冒号

Using colons in querySelectorAll()

本文关键字:querySelectorAll      更新时间:2023-09-26

我正试图使用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

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

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

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

当前版本的问题是,除非使用引号,否则值必须符合对标识符的限制,而事实并非如此。