最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using colons in querySelectorAll() - Stack Overflow

programmeradmin0浏览0评论

I'm trying to use the querySelectorAll() method to grab links in web pages, but I want to ignore links that begin with "javascript:" or use another protocol like "itpc://"

Is there any way to include these in a "not()" pseudo selector?

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

Even though the first method works fine on the current page, there's no guarantee that it will work on every page I'll be using it on, so I'd really like to be able to detect that colon.

I'm trying to use the querySelectorAll() method to grab links in web pages, but I want to ignore links that begin with "javascript:" or use another protocol like "itpc://"

Is there any way to include these in a "not()" pseudo selector?

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

Even though the first method works fine on the current page, there's no guarantee that it will work on every page I'll be using it on, so I'd really like to be able to detect that colon.

Share Improve this question edited Mar 19, 2012 at 17:06 BoltClock 725k165 gold badges1.4k silver badges1.4k bronze badges asked Mar 18, 2012 at 23:38 Jesse FultonJesse Fulton 2,2181 gold badge24 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Based on the spec, turning the values you want to target into strings will work:

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

The problem with your current version is that unless you use quotes the values have to conform to the restrictions placed on identifiers, which they do not.

发布评论

评论列表(0)

  1. 暂无评论