gpt4 book ai didi

javascript - document.evaluate - 跨浏览器?

转载 作者:可可西里 更新时间:2023-11-01 01:42:26 24 4
gpt4 key购买 nike

我一直在寻找 Sizzle 以外的 CSS 选择器函数我遇到了this function .

function SparkEn(xpath,root) {
xpath = xpath
.replace(/((^|\|)\s*)([^/|\s]+)/g,'$2.//$3')
.replace(/\.([\w-]+)(?!([^\]]*]))/g, '[@class="$1" or @class$=" $1" or @class^="$1 " or @class~=" $1 "]')
.replace(/#([\w-]+)/g, '[@id="$1"]')
.replace(/\/\[/g,'/*[');
str = '(@\\w+|"[^"]*"|\'[^\']*\')';
xpath = xpath
.replace(new RegExp(str+'\\s*~=\\s*'+str,'g'), 'contains($1,$2)')
.replace(new RegExp(str+'\\s*\\^=\\s*'+str,'g'), 'starts-with($1,$2)')
.replace(new RegExp(str+'\\s*\\$=\\s*'+str,'g'), 'substring($1,string-length($1)-string-length($2)+1)=$2');
var got = document.evaluate(xpath, root||document, null, 5, null);
var result=[];
while (next = got.iterateNext())
result.push(next);
return result;
}

我只是觉得它好得令人难以置信,这是 firefox 独有的功能(xpath?)还是它很慢?基本上为什么我要用 Sizzle 来解决这个问题?

最佳答案

我相信no stable version of IE supports document.evaluate ,因此您只能使用其他所有浏览器。它并不慢,因为它是 XPath 的 native 实现。

Sizzle 很有用,因为它在可用时使用浏览器提供的 native 支持(例如 document.getElementsByClassName ),但在不可用时回退到自己执行 (IE)。它也被 jQuery 使用和 Prototype ,因此它经过了严格、严格的测试,不太可能给您带来任何麻烦。 Sizzle 也经过了大量的速度测试和优化(他们有一个完整的 speed test suite ),这是您不必做的更多工作。

我会说使用 jQuery、Prototype 或仅使用 Sizzle 本身,除非您正在做一些对性能非常敏感的事情(老实说,这可能表明您的应用程序结构不佳)。

关于javascript - document.evaluate - 跨浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4681968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com