如何在 JavaScript 中测试 CSS 选择器?
我如何测试 CSS1-3 选择器以检查它们是否获得正确的元素,例如使用 JavaScript(也许是 jQuery)?
How could I test CSS1-3 selectors to check that they get the correct elements, e.g. with JavaScript (maybe jQuery)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
到目前为止,最简单的传统方法是根本不使用 JavaScript,而只需手动设置一个测试页面,您可以在其中根据自己的喜好测试选择器。您在 Web 上看到的测试用例(例如著名的 CSS3.info 选择器测试)实际上只是在线托管的增强版。
但如果您正在寻找 JavaScript 方法,可以尝试选择器 API。它可用于现代 DOM 实现(IE8+ 和其他),并提供一个 JavaScript 前端,用于使用 CSS 选择器查询 DOM 中的元素节点,以及测试给定浏览器本机支持的 CSS 选择器。
(对于没有实现 Selectors API 的浏览器,您必须依赖 jQuery,但请记住,它提供了对与浏览器支持不同的选择器集以及其自己的非标准扩展的支持在选择器规范中找不到这些内容。将 jQuery 与 Chrome 的 JavaScript 控制台结合使用的示例。测试选择器可以找到此处。)
根据您的需要调用
querySelector()
或querySelectorAll()
测试,并检查返回值(最好在浏览器的开发人员工具中,因为您只是测试):如果找到匹配项,前一个方法将返回第一个匹配的
Element
,而后者将返回所有匹配的Element
作为NodeList
匹配的元素。如果没有找到,前者返回
null
,而后者返回一个空的NodeList
。如果选择器无效,将抛出一个您可以捕获的异常。
以下是 Firefox 10 上 Firebug 控制台中命令编辑器(多行)的一些示例,已在 这个问题:
在
body
中查找第一个h1
:查询我们刚刚找到的
h1
元素的后代:< /p>在 Firefox 中测试
:-moz-any()
伪类 (:-webkit-any()
(Safari/Chrome 中):测试一个不存在的选择器(也许我们中的许多人希望确实存在):
The simplest traditional way by far is to not use JavaScript at all, and just set up a test page by hand where you can test selectors to your heart's content. The test cases you see on the Web (like the well-known CSS3.info Selectors Test) are really just souped-up versions hosted online.
But if you're looking for a JavaScript method, you can try the Selectors API. It's available in modern DOM implementations (IE8+ and others) and it provides a JavaScript frontend for querying the DOM for element nodes using CSS selectors, as well as testing CSS selectors natively supported by a given browser.
(For browsers that don't implement the Selectors API, you'll have to rely on jQuery, but remember that it provides support for a different set of selectors than what a browser supports as well as its own non-standard extensions which aren't found in the Selectors spec. An example of using jQuery with Chrome's JavaScript console to test a selector can be found here.)
Call
querySelector()
orquerySelectorAll()
depending on what you want to test, and check the return value (preferably in your browser's developer tools since you're just testing):If matches are found, the former method returns the first
Element
matched while the latter returns all elements matched as aNodeList
.If nothing is found, the former returns
null
while the latter returns an emptyNodeList
.If the selector is invalid, an exception will be thrown which you can catch.
Here are some examples with the command editor (multiline) in Firebug's console on Firefox 10, tested on this very question:
Finding the first
h1
inbody
:Querying descendants of that
h1
element we just found:Testing the
:-moz-any()
pseudo-class in Firefox (:-webkit-any()
in Safari/Chrome):Testing a nonexistent selector (that perhaps many of us wish did exist):
http://selectorgadget.com 非常适合测试和构建 CSS 选择器。只需将他们提供的一段 JavaScript 拖放到您的书签栏中,然后在需要时单击它即可。
http://selectorgadget.com is quite nice to test and build CSS selectors. Just drag and drop a piece of JavaScript they provide into your bookmarks bar and click it whenever you need.