Table of Contents
-
Home Web Front-end H5 Tutorial 0基础接触html5之jquery(二)选择器

0基础接触html5之jquery(二)选择器

May 17, 2016 am 09:08 AM

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)!


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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

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.

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.

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 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

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.

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.

See all articles