Table of Contents
回复内容:
Home Web Front-end H5 Tutorial html5推出queryselectall之后,jQuery的作用是否减小了?

html5推出queryselectall之后,jQuery的作用是否减小了?

Jun 07, 2016 am 08:43 AM
html5 javascript jquery query

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>
Copy after login
题主的说法是对的,必要性显然是减小了。DOM 的接口每方便一点,引入 jQuery 的优势就减小一点。优势减小不等于没用嘛。 参考这个:jQuery会过时吗? - 贺师俊的回答 单就选择器来说,jquery的作用减小了。

没有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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

See all articles