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

查看:87
本文介绍了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;
}

我只是觉得这样太好了,是不是真的,这是一个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 ,所以你被限制到其他浏览器。它不是很慢,因为它是XPath的本机实现。

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 原型,所以它经过严格的测试,不太可能给出你有什么麻烦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,原型或只是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天全站免登陆