document.evaluate - 跨浏览器? [英] document.evaluate - Cross browser?

查看:452
本文介绍了document.evaluate - 跨浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找除 Sizzle 之外的CSS选择器函数,我遇到了此函数

I have been looking for a CSS selector function other than Sizzle and I have come across 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;
}


b $ b

我只是觉得这是太好了,是真的,这是一个firefox唯一的函数(xpath?)还是它慢?基本上为什么我会使用Sizzle这个?

I just feel like it is too good to be true, is this a firefox only function (xpath?) or is it slow? Basically why would I use Sizzle over this?

推荐答案

我相信没有稳定版本的IE支持 document.evaluate ,所以你只能使用其他浏览器。

I believe no stable version of IE supports document.evaluate, so you're limited to every other browser. It's not slow since it's a native implementation of XPath.

Sizzle非常有用,因为它使用本地支持浏览器提供的功能(例如 document.getElementsByClassName ),但是在不可用时(IE)会自动执行。 jQuery Prototype ,所以它经过严格测试,不太可能给你麻烦。 Sizzle还经过了高度的速度测试和优化(他们有一个完整的速度测试套件) ),这是你不必做的更多的工作。

Sizzle is useful because it uses the native support browsers offer when available (such as document.getElementsByClassName), but falls back to doing it itself when unavailable (IE). It's also used by jQuery and Prototype, so it's heavily, heavily tested and is unlikely to give you any trouble. Sizzle is also heavily speed-tested and optimized (they have a whole speed test suite), which is more work you don't have to do.

我会说用jQuery,Prototype,或者只是Sizzle本身,除非你做一些令人难以置信的东西性能敏感(实际上,这可能是一个指标,说明您的应用程序结构不良)。

I'd say go with jQuery, Prototype, or just Sizzle by itself unless you are doing something incredibly performance-sensitive (which, honestly, is probably an indicator that you've structured your application poorly).

这篇关于document.evaluate - 跨浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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