問題描述
XPath 可以做所有 querySelector 可以做的事情,甚至更多,那么你什么時候會選擇后者呢?我還沒有看到任何速度基準比較這兩者,所以現(xiàn)在我根據(jù)語法簡潔性進行選擇,這似乎有點隨意.
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 創(chuàng)建的 Sizzle.Sizzle 也是 jQuery 中使用的主要選擇器引擎.它在適當?shù)牡胤绞褂?querySelector,在 querySelector 不可用的地方使用普通的 DOM 方法.
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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!