XPath或querySelector? [英] XPath or querySelector?

查看:499
本文介绍了XPath或querySelector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XPath可以做一切querySelector可以做的事情,等等,所以什么时候你会选择后者?我没有看到任何速度基准比较两者,所以现在我选择基于语法简洁,这似乎是任意的。

XPath can do everything querySelector can do, and more, so when would you ever choose the latter? I haven't seen any speed benchmarks comparing the two, so right now I'm choosing based on syntax conciseness, which seems kind of arbitrary.

编辑:我可能应该已经声明我正在为Firefox编写Greasemonkey脚本,所以我不担心跨浏览器兼容性,而宁愿不包括任何库。

I probably should have stated that I'm writing Greasemonkey scripts for Firefox, so I'm not worried about cross-browser compatibility, and would rather not include any libraries.

推荐答案

您使用的是什么浏览器?在Safari(或iPhone)中,querySelector和querySelectorAll比XPath快得多。 IE根本不支持XPath,IE6和IE7不支持querySelector。最快的跨浏览器选择器引擎是由John Resig创建的 Sizzle 。 Sizzle也是jQuery中使用的主要选择器引擎。它在正确的DOM方法中使用querySelector,其中querySelector不可用。

What browser are you using? In Safari (or the iPhone), querySelector and querySelectorAll are much faster than XPath. IE doesn't support XPath at all, and IE6 and IE7 don't support querySelector. The fastest cross-browser selector engine is Sizzle, created by John Resig. Sizzle is also the main selector engine used in jQuery. It uses querySelector where appropriate and normal DOM methods where querySelector is unavailable.

这篇关于XPath或querySelector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆