javascript - Sizzle 和 document.querySelectorAll 有什么区别

标签 javascript sizzle queryselector

据我所知,Sizzle 和 querySelector/querySelectorAll 是 CSS 选择器。 那么...加载 Sizzle 和做之间有什么区别:

Sizzle("my CSS query")

document.querySelectorAll("my CSS query")

此外,我知道 Sizzle 返回一个元素数组,而 querySelectorAll 返回一个 NodeList(在大多数浏览器中)。我还知道您需要加载 Sizzle,并且您只能对一个元素使用 document.querySelector

那么在性能上还有什么其他差异吗?

编辑:我的问题只是关于 Sizzle 选择器引擎(和 querySelectorAll)的问题。请不要涉及 jQuery。

最佳答案

Sizzle 是在 querySelectorAll 不存在的时候创建的。它的开发在 querySelectorAll 引入后继续进行,以通过 querySelectorAll 的早期实现来解决浏览器错误。

Sizzle 本身会尝试直接使用 querySelectorAll 并且只会使用它自己的 DOM 遍历,如果不支持选择器或者知道给定浏览器版本有问题.因此,对于现代浏览器而言,性能上应该不会有明显差异,因为在这两种情况下都会使用 querySelectorAll

querySelectorAll 相比,Sizzle 允许定义自定义伪选择器,缺点是您无法从如今 querySelectorAll 提供的性能中获益.

所以现在,如果您不需要自定义伪选择器,就不再需要 Sizzle 了。仅当您需要针对已知存在错误的旧浏览器版本时才使用它。

关于javascript - Sizzle 和 document.querySelectorAll 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60685782/

相关文章:

javascript - noConflict JQuery 的 Drupal 问题

javascript - 验证表单中的多个字段

javascript - jQuery - 查找其中包含多个 HTML 标记属性的元素

jquery - 使用 jQuery 设置表格单元格样式

javascript - 如何为根节点的直接子节点创建选择器?

javascript - 用于通过::after 修饰符添加的内容的 DOM 选择器

javascript - 如何确定弹出窗口是否成功加载?

javascript - 尝试理解这段代码(发布/订阅库)

jQuery 1.4.4 : How to find an element based on its data-attribute value?

javascript - 相邻同级选择器 element.querySelector ("+element") 无效