jQuery中的选择器selecor是大家经常使用的强大功能,事实上jQuery提供给我们非常丰富的手段来使用选择器定位DOM元素。 下面先简单介绍一下用法
选择器 实例 选取
* $("*") 所有元素
#id $("#lastname") id="lastname" 的元素
.class $(".intro") 所有 class="intro" 的元素
element $("p") 所有
元素
.class.class $(".intro.demo") 所有 class="intro" 且 class="demo" 的元素
:first $("p:first") 第一个
元素
:last $("p:last") 最后一个
元素
:even $("tr:even") 所有偶数
元素
dd $("trdd") 所有奇数
元素
:eq(index) $("ul li:eq(3)") 列表中的第四个元素(index 从 0 开始)
:gt(no.) $("ul li:gt(3)") 列出 index 大于 3 的元素
:lt(no) $("ul li:lt(3)") 列出 index 小于 3 的元素
:not(selector) $("input:not(:empty)") 所有不为空的 input 元素
:header $(":header") 所有标题元素
-
:animated 所有动画元素
:contains(text) $(":contains('str')") 包含指定字符串的所有元素
:empty $(":empty") 无子(元素)节点的所有元素
:hidden $("p:hidden") 所有隐藏的
元素
:visible $("table:visible") 所有可见的表格
s1,s2,s3 $("th,td,.intro") 所有带有匹配选择的元素
[attribute] $("[href]") 所有带有 href 属性的元素
[attribute=value] $("[href='#']") 所有 href 属性的值等于 "#" 的元素
[attribute!=value] $("[href!='#']") 所有 href 属性的值不等于 "#" 的元素
[attribute$=value] $("[href$='.jpg']") 所有 href 属性的值包含以 ".jpg" 结尾的元素
:input $(":input") 所有元素
:text $(":text") 所有 type="text" 的元素
:password $(":password") 所有 type="password" 的元素
:radio $(":radio") 所有 type="radio" 的元素
:checkbox $(":checkbox") 所有 type="checkbox" 的元素
:submit $(":submit") 所有 type="submit" 的元素
:reset $(":reset") 所有 type="reset" 的元素
:button
$(":button") 所有 type="button" 的元素
:image
$(":image") 所有 type="image" 的元素
:file $(":file") 所有 type="file" 的元素
:enabled $(":enabled") 所有激活的 input 元素
:disabled $(":disabled") 所有禁用的 input 元素
:selected $(":selected") 所有被选取的 input 元素
:checked $(":checked") 所有被选中的 input 元素
虽然选择器是Jquery最常用也是最基础的功能,但是很少有开发人员会考虑使用不同的选择器来处理性能问题。这里我们将介绍几种常用的选择器,及其它们之间的性能差异。
1.$("#id")
使用id来定位DOM元素无疑是最佳提高性能方式,因为jQuery底层将直接调用本地方法document.getElementbyId(),如果熟悉javascript的朋友应该了解这个方法将直接通过元素id来返回对应的元素。
当然,如果这个方式不能直接找到你需要的元素,那么你可以考虑调用.find()方法。如下:
$("#gbid").find("p")
使用以上方法可以有效的缩小你定位的DOM元素。
2.$("p"),$("p"),$("input")
使用标签名是第二优化选择,因为jQuery将直接调用本地方法document.getElementsByTagname()来定位DOM元素。
3.$(".class")
使用.class方法对于我们来说稍微复杂些,对于比较新的浏览器例如,IE9,它支持本地方法document.getElementsByClassName(),而对于老的浏览器,例如,IE8或者更早版本来说,不得不使用DOM搜索方式来实现,自然会对于性能产生比较大的影响。所以大家得选择使用。
4.$("[attribute=value]")
对于利用属性来定位DOM元素,本地javascript方法中并没有直接的实现。所以大都都是使用DOM搜索方式来达到效果,很多现代浏览器支持querySelectorAll()方法,但是不同浏览器的性能还是有区别。总体来说,使用这种方式来定位DOM元素,并不是非常理想。所以为了获得更好的优化效果,你需要尽量避免这个对性能有害的使用方式。
5.$(":hidden")
和上面使用属性来定位DOM的方式类似,这种伪选择器也同样没有本地js方法来直接实现。而且jQuery需要搜索每一个元素来定位这个选择器,将会对你的应用带来比较大的性能问题。所以大家尽量不要使用,当然,如果你非要使用的话,请先是用find方法定位父元素,然后,再使用这个选择器,这样会帮助你很好的优化性能,如下:$("#gbp").find(":hidden")
以上是一个基本使用选择器的规则,自上而下性能依次下降,如果大家在开发中使用选择器,请遵循以上这个简单的优化性能规则。
以上就是0基础接触html5之jquery(二)选择器的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment