html5推出queryselectall之后,jQuery的作用是否减小了?
jQuery就是javascript和query(查询),html5推出queryselectall之后,可以更快的使用选择器查询了,jQuery的必要性是不是就没那么强了?
回复内容:
首先要弄清楚,querySelectorAll返回的是原生的元素集合,而jQuery的$函数返回的是jQuery对象。大家之所以更愿意用jQuery,很大程度上是因为jQuery对象则提供了一系列简单的DOM操作接口。以“查询所有.section元素,并将其移除”这个功能为例。如果用querySelectorAll,得这么写:
<span class="kd">var</span> <span class="nx">sections</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelectorAll</span><span class="p">(</span><span class="s1">'.section'</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="nx">sections</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">>=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="nx">sections</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">parentNode</span><span class="p">.</span><span class="nx">removeChild</span><span class="p">(</span><span class="nx">sections</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> <span class="p">}</span>
没有querySelector和querySelectorAll之前,DOM提供的selector功能太有限,getElementById只能取ID,getElementsByTagName只能取节点的名字,getElementsByClassName只能取class名称。要完成复杂的选择器“.className .subclassName p > a”要写很多代码才行。然后JS的框架出现了,css selector逐渐被实现成DSL,querySelector和querySelectorAll把各种框架都支持的css selector port进了DOM,客观上所有框架的这部分功能的作用都减小了;至少复杂的DOM操作,jquery等不是必然选择了。 题主要想清楚一个事情,jq不光光是提供了更好用的接口,最关键还是兼容性的问题 Sizzle 2000行, jQuery 9000行,你觉得能一样么 Sizzle懂了的话,就不这么看了 对于用原生的人方便不少,但是对于不同浏览器的差异,jq,zepto。。 始终需要。。 如果你去看一下jquery的查询引擎sizzle的代码,它内部本来就使用了querySelectorAll,用sizzle的好处自然是更多的功能和更好的封装 jquery分为
1.x还有2.x版本,很大一个区别就是选择器直接采用了querySelectorAll

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
