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的更多相关文章

  1. 【小贴士】zepto find元素以及ios弹出键盘可能让你很头疼

    前言 在此,我不得不说移动端的兼容问题很多,并且很令人头疼,这不,这个星期又有两个让我逮着了,一个是使用zepto过程中出现的问题,一个是ios虚拟键盘的问题 我这里做一次记录,以免以后忘了,同时希望 ...

  2. Sizzle源码分析 (一)

    Sizzle 源码分析 (一) 2.1 稳定 版本 Sizzle 选择器引擎博大精深,下面开始阅读它的源代码,并从中做出标记 .先从入口开始,之后慢慢切入 . 入口函数 Sizzle () 源码 19 ...

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

    querySelector和querySelectorAll是W3C提供的新的查询接口 目前几乎主流浏览器均支持了他们.包括 IE8(含) 以上版本. Firefox. Chrome.Safari.O ...

  4. querySelector和querySelectorAll

    jQuery被开发者如此的青睐和它强大的选择器有很大关系,比起笨重的document.getElementById.document.getElementByName… ,查找元素很方便,其实W3C中 ...

  5. CSS选择器比较:queryselector queryselectorall

    官网解释: querySelector() and querySelectorAll() are two JavaScript functions very useful when working w ...

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

    效果图废话不多直接上代码 用的avalon渲染,其实都是一样的 <div class="shop-arithmetic"> <a href="javas ...

  7. ES6 Arrow Function & this bug

    ES6 Arrow Function & this bug let accHeadings = document.querySelectorAll(`.accordionItemHeading ...

  8. IE6、IE7兼容querySelectorAll和querySelector方法

    querySelector 和 querySelectorAll 方法是 W3C Selectors API 规范中定义的.他们的作用是根据 CSS 选择器规范,便捷定位文档中指定元素. 目前几乎主流 ...

  9. event duplication bind bug & h5 dataset flag solution

    event duplication bind bug & h5 dataset flag solution https://codepen.io/xgqfrms/full/PaRBEy/ OK ...

随机推荐

  1. C#中byte[] 与指针

    本文假定读者熟悉byte[].指针. C#是类型安全的,默认是不允许使用指针,但是针对C\C++或者其他语言的程序员(delphi)转为使用C#的的工作人员,不适用指针觉得很别扭.下面介绍一下基础的指 ...

  2. Java基础笔记-多线程

    线程: 方式一:继承Thread类并且复写run方法. 格式: class MyThread extends Thread { public void run() { 线程中要运行的代码. } } 其 ...

  3. RDLC报表系列(六) 多图表-折线图和柱状图

    美好的一天开始了,这篇是RDLC系列的最后一篇文章,我的小项目也已经release,正在测试中. 1.新建demo3.aspx和demo3.rdlc文件 2.往rdlc文件中拖一个图标控件,在弹出的窗 ...

  4. oracle 定义数据完整性

    1. 定义主键约束 1.1 在创建表时定义主键约束 create table student(name varchar2(8),studentid varchar2(10) primary key,s ...

  5. iOS 类管理

    CocoaPods安装和使用教程 Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaP ...

  6. TextField的所有属性和方法(转)

    转自:http://tsyouaschen.iteye.com/blog/600255 表一 TextField 对象的方法 方法 说明TextField.addListener 加入接收触发事件如文 ...

  7. codevs1145

    题目描述                     Description 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆 ...

  8. Oracle 添加 scott 示例用户

    学习SQL有一段时间了,但是也忘记的差不多了,今天有赶紧复习复习,然后发现一个问题,为啥之前看的视频教程,马士兵用的Oracle有scott用户和那些表格,而我的没有?难道是Oracle取消了?然后百 ...

  9. oracle distinct 去除重复,同时按某字段排序

    SELECT distinct supplier_id, min(evidence_date) as evidence_date FROM TD_SUPPLIER_EVIDENCE_INFO wher ...

  10. scikit-learn的主要模块和基本使用

    1.加载数据(Data Loading) 假设输入是特征矩阵或者csv文件,首先数据被载入内存. scikit-learn的实现使用了NumPy中的arrays,所以,使用NumPy来载入csv文件. ...