querySelectorAll的BUG

querySelector和querySelectorAll是W3C提供的新的查询接口

目前 IE8/9及Firefox/Chrome/Safari/Opera 的最新版已经支持它们。

但是Element.querySelectorAll有个bug

  

<div>
    <div id="bar">
<span>
<a href="###">asfd</a>
</span>
</div>
</div>

  

var bar = document.getElementById(‘bar‘);
console.log(bar.querySelectorAll(‘div span a‘).length); // 1
console.log(bar.querySelectorAll(‘span a‘).length); // 1
console.log($(‘div span a‘,bar)); // jQuery没有选择到

div span a 这里本该选择不到的,结果却选择到了

下面是ExtJS的兼容实现:

try {
/*
* This checking here is to "fix" the behaviour of querySelectorAll
* for non root document queries. The way qsa works is intentional,
* however it‘s definitely not the expected way it should work.
* When descendant selectors are used, only the lowest selector must be inside the root!
* More info: http://ejohn.org/blog/thoughts-on-queryselectorall/
* So we create a descendant selector by prepending the root‘s ID, and query the parent node.
* UNLESS the root has no parent in which qsa will work perfectly.
*
* We only modify the path for single selectors (ie, no multiples),
* without a full parser it makes it difficult to do this correctly.
*/
            // startIdRe = /^\s*#/;

if (root.parentNode && (root.nodeType !== 9) && path.indexOf(‘,‘) === -1 && !startIdRe.test(path)) {
path = ‘#‘ + Ext.escapeId(Ext.id(root)) + ‘ ‘ + path;
root = root.parentNode;
}
return single ? [ root.querySelector(path) ]
: Ext.Array.toArray(root.querySelectorAll(path));
}
catch (e) {
}

  

如果root有父节点,并且root不是document,并且只是单个选择,并且选择文本中不以#开头,那么就将root赋值上ID,进行查询

比如:bar.querySelectorAll(‘div span a‘)----> ‘#bar div span
a‘,进行纠正,但是不支持多个的选择(比如div span a,p div span)

再将root换成root的父节点,这样就解决了问题

Ext.id 方法是获得Element的ID,如果没有ID属性,则赋值一个

Ex.escapeId 是将不合法的ID值进行一个过滤

以后再研究jQuery关于这个BUG的处理。

querySelectorAll的BUG

时间: 2024-08-04 10:07:03

querySelectorAll的BUG的相关文章

document.querySelector和querySelectorAll方法

querySelector和querySelectorAll是W3C提供的新的查询接口,其主要特点如下: 1.querySelector只返回匹配的第一个元素,如果没有匹配项,返回null. 2.querySelectorAll返回匹配的元素集合,如果没有匹配项,返回空的nodelist(节点数组). 3.返回的结果是静态的,之后对document结构的改变不会影响到之前取到的结果. 这两个方法都可以接受三种类型的参数:id(#),class(.),标签,很像jquery的选择器. var ob

sizzle分析记录:关于querySelectorAll兼容问题

querySelector和querySelectorAll是W3C提供的新的查询接口 目前几乎主流浏览器均支持了他们.包括 IE8(含) 以上版本. Firefox. Chrome.Safari.Opera. 万能的sizzle在高版本的浏览器中复杂的选择器尽量走querySelectorAll,前提是这个匹配的节点没有兼容问题 从IE8开始虽然支持querySelectorAll的API,但是会有各式各样的BUG,所以sizzle拿rbuggyQSA用来记录这个BUG问题 if ( supp

js jquery 权限单选 bug修改以及正确代码 购物车数量加减

效果图废话不多直接上代码 用的avalon渲染,其实都是一样的 <div class="shop-arithmetic"> <a href="javascript:;" class="minus" onclick="delpubls(this)">-</a> <input type="number" style="min-height: 20px;font

querySelector和querySelectorAll

jQuery被开发者如此的青睐和它强大的选择器有很大关系,比起笨重的document.getElementById.document.getElementByName… ,查找元素很方便,其实W3C中提供了querySelector和querySelectorAll查询接口已经实现了类似功能. 定义 其实这两个方法看名字就能明白什么意思,不过还是引用一下W3C的解释 querySelector:return the first matching Element node within the n

JS中querySelectorAll的作用

//外部页切换 var clickme=document.getElementById('navbox').getElementsByTagName('button'); var box=document.getElementById('itembox').querySelectorAll('.boxStyle'); for(var i=0;i<clickme.length;i++){ clickme[i].index=i; clickme[i].onclick=function(){ for(

解决myeclipse中struts2 bug问题包的替换问题

因为struts2的bug问题,手工替换还是比较麻烦,但即便是最新的myeclipse2014也没有替换最新的struts2包,研究了一天,终于找到了解决办法.以下就解决方法与大家分享一下. 1.在perferences中找到 Myeclipse->Project Libraries,右边找到 struts2.1 Libraries,点击 Enable advanced configiguration,去掉以下文件前面的对勾,然后点击 Add custom Jars 2.在弹出的对话框中选择 A

关于工作中微信分享接口的bug

调用config 接口1的时候传入参数 debug: true 可以开启debug模式,页面会alert出错误信息.以下为常见错误及解决方法: invalid url domain当前页面所在域名与使用的corpid没有绑定(可在该企业号的应用可信域名中配置域名). invalid signature签名错误.建议按如下顺序检查: 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 页面工具进行校验. 确认c

android 2.x上Dialog特定情况导致无法dismiss的bug

近期在做一个简单的对话框框架,作用不过做出一个显示效果在android 2.x到android 4.x上均类似的对话框. 思路比較简单,就是使用装饰模式包裹Dialog,暴露的接口调用时会自己主动完毕一些设置 可是遇到一个特别诡异的问题,在android 2.x上的Dialog对话框无法被dismiss,仅仅要弹出来界面就卡死,强行使用eclipse又一次run的话会导致android设备黑屏无响应,假设是模拟器,则直接崩溃,看到启动画面.并且这个还不是ANR错误. 后来通过删减代码找到了问题的

ThinkPHP3.2.3 foreach嵌套循环bug

有两个数组authA和authB authA如下 1 Array 2 ( 3 [0] => Array 4 ( 5 [auth_id] => 1 6 [auth_name] => 文章管理 7 [auth_pid] => 0 8 [auth_c] => 9 [auth_a] => 10 [auth_level] => 0 11 ) 12 13 [1] => Array 14 ( 15 [auth_id] => 4 16 [auth_name] =>