这是看《锋利的jquery》时,整理出来的一些东西,很多方法,需要大家亲自实践一下,才会理解得更加深刻,切莫眼高手低哦……
Jquery选择器分类:基本选择器,层次选择器,过滤选择器,表单选择器
一、基本选择器:jquery中最常用的选择器,也是最简单的选择器。通过元素id、class和标签名等来查找DOM元素。
表-基本选择器
选择器 |
描述 |
返回 |
示例 |
#id |
根据给定的id匹配一个元素 |
单个元素 |
$(“#test”)选取id为test的元素 |
.class |
根据给定的类名匹配元素 |
集合元素 |
$(“.test”)选取所有class为test的元素 |
element |
根据给定的的元素名匹配元素 |
集合元素 |
$(“p”) 选取所有的元素 |
* |
匹配所有元素 |
集合元素 |
$(“*”)选取所有元素 |
Selector1,Selector2, ……, SelectorN |
将每一个选择器匹配到的元素合并后一起返回 |
集合元素 |
$(“div,span,p.myClass”)选取所有 |
二、层次选择器:通过DOM元素之间的层次关系获取特定元素,如后代元素、子元素、相邻元素、兄弟元素等。层次选择器是一个很好的选择
表-层次选择器
选择器 |
描述 |
返回 |
示例 |
$(“ancestor descendant”) |
选取ancestor元素里所有descendant(后代)元素 |
集合元素 |
$(“div span”)选取 |
$(“parent>child”) |
选取父元素下的子元素 |
集合元素 |
$(“div>span”)选取 |
$(‘prev+next') |
选取紧接在prev元素后的next元素 |
集合元素 |
$(‘.one+div') 选取class为one的下一个 |
$(‘prev~siblings') |
选取prev元素之后的所有元素 |
集合元素 |
$(‘.one+div') 选取class为one的元素后面所有 |
注意:
$(‘prev+next')选择器与next()方法的等价关系
$(‘.one+div') 等价于 $(“.one”).next(“div”)
$(‘prev~siblings')选择器与nextAll()方法的等价关系
$(‘.one~div') 等价于 $(“.one”).nextAll(“div”)
三、过滤选择器:主要是通过特定的过滤选择器规则来筛选出所需的DOM元素,过滤规则与css中伪类选择器语法相同,即选择器都以一个冒号(:)开头。
按照不同的过滤规则,过滤选择器分为:基本过滤选择器、内容过滤选择器、可见性过滤选择器、属性过滤选择器、子元素过滤选择器、表单对象属性座率选择器
表1-基本过滤选择器
选择器 |
描述 |
返回 |
示例 |
:first |
选取第一个元素 |
单个元素 |
$(“div:first”)选取所有 |
:last |
选取最后一个元素 |
单个元素 |
$(“div:last”)选取所有 |
:not(selector) |
去除所有与给定选择器匹配的元素 |
集合元素 |
$(“input:not(.myClass)”) 选取class为不是myClass的元素 |
:even |
选取索引是偶数的所有元素,索引从0开始 |
集合元素 |
$(“input:even”) 选取索引是偶数的元素 |
:odd |
选取索引是奇数的所有元素,索引从0开始 |
集合元素 |
$(“input:odd”) 选取索引是奇数的元素 |
:eq(index) |
选取索引等于index的元素(index从0开始) |
单个元素 |
$(“input:eq(1)”选取索引为1的元素 |
:gt(index) |
选取索引大于index的元素(index从0开始) |
集合元素 |
$(“input:gt(1)”) 选取索引大于1的元素(注:大于1,而不包括1) |
:lt(index) |
选取索引小于index的元素(index从0开始) |
集合元素 |
$(“input:gt(1)”) 选取索引小于1的元素(注:小于1,而不包括1) |
:header |
选取所有的标题元素,例如h1,h2,h3等待 |
集合元素 |
$(“:header”)选取网页中所有的,,…… |
:animated |
选取当前正在执行动画的所有元素 |
集合元素 |
$(“div:animated”)选取正在执行动画的 |
表2-内容过滤选择器
选择器 |
描述 |
返回 |
示例 |
:contains(text) |
选取含有文本内容为”text”的元素 |
集合元素 |
$(“div:contains(‘我')”)选取含有文本“我”的 |
:empty |
选取不包含子元素或者文本的空元素 |
集合元素 |
$(“div:empty”)选取不包含子元素(包括文本元素)的 |
:has(selector) |
选取含有选择器所匹配的元素的元素 |
集合元素 |
$(“div:has(p)”) 选取含有元素的 |
:parent |
选取含有子元素或者文本元素 |
集合元素 |
$(“div:parent”) 选取拥有子元素(包括文本元素)的 |
表3-可见性过滤选择器
选择器 |
描述 |
返回 |
示例 |
:hidden |
选取所有不可见的元素 |
集合元素 |
$(“:hidden”)选取所有不可见的元素。包括, |
:visible |
选取不包含子元素或者文本的空元素 |
集合元素 |
$(“div:visible”)选取所有可见的 |
表4-属性过滤选择器
选择器 |
描述 |
返回 |
示例 |
[attribute] |
选取拥有此属性的元素 |
集合元素 |
$(“div[id]”)选取拥有属性id的元素 |
[attribute=value] |
选取属性的值为value的元素 |
集合元素 |
$(“div[title=test]”)选取属性title为”test”的 |
[attribute!=value] |
选取属性的值不等于value的元素 |
集合元素 |
$(“div[title!=test]”)选取属性title不等于”test”的 |
[attribute^=value] |
选取属性的值以value开始的元素 |
集合元素 |
$(“div[title^=test]”)选取属性title以”test”开始的 |
[attribute$=value] |
选取属性的值以value结束的元素 |
集合元素 |
$(“div[title$=test]”)选取属性title以”test”结束的 |
[attribute*=value] |
选取属性值含有value的元素 |
集合元素 |
$(“div[title*=test]”)选取属性title含有”test”的 |
[selector1][selector2][selectorN] |
用属性选择器合并成一个复合属性选择器,满足多个条件。每个选择一次,缩小一次范围 |
集合元素 |
$(“div[id][title$='tets']”)选取拥有属性id,并且属性title以”test”结束的 |
表5-子元素过滤选择器
选择器 |
描述 |
返回 |
示例 |
:nth-child(index/even/odd/equation) |
选取每个父元素下的第index个子元素或者奇偶元素(index从1算起) |
集合元素 |
:eq(index)只匹配一个元素,而:nth-child将为每一个父元素匹配子元素,并且:nth-child(index)的index是从1开始的,而:eq(index)是从0算起 |
:first-child |
选取每个父元素的第一个子元素 |
集合元素 |
:first只返回单个元素,而:first-child选择符将为每个父元素匹配第一个子元素 |
:last-child |
选取每个父元素的最后一个子元素 |
集合元素 |
:last只返回单个元素,而:last-child选择符将为每个父元素匹配最后一个子元素 |
:only-child |
如果某个元素是它父元素中唯一的子元素,那么将会被匹配。如果父元素中含有其他元素,则不会被匹配 |
集合元素 |
|
:nth-child()选择器详细功能
(1):nth-child(even)能选取每个父元素下的索引值是偶数的元素。
(2):nth-child(odd) 能选取每个父元素下的索引值是奇数的元素。
(3):nth-child(2)能选取每个父元素下的索引值等于2的元素
(4):nth-child(3n)能选取每个父元素下的索引值是3的倍数的元素,(n从0开始)。
(5):nth-child(3n+1)能选取每个父元素的索引值是(3n+1)的元素。(n从0开始)
表6-表单对象属性过滤选择器
选择器 |
描述 |
返回 |
示例 |
:enabled |
选取所有可用元素 |
集合元素 |
$(“#form1:enabled”) ;选取id为”form1”的表单内的所有可用元素 |
:disabled |
选取所有不可用元素 |
集合元素 |
$(“#form1:disabled”) ;选取id为”form1”的表单内的所有不可用元素 |
:checked |
选取所有被选中的元素(单选框,复选框) |
集合元素 |
$(“input:checked”);选取所有被选中的元素 |
:selected |
选取所有被选中的选项元素(下拉列表) |
集合元素 |
$(“select:selected”);选取所有被选中的选项元素 |
4. Form Selector
Table-Form object attribute filtering example
|
Description | Return | Example | ||||||||||||||||||||||||||||||||||||||||||||||||
:input |
Select all available elements |
Collection elements |
$(“:input”) Select all , |
||||||||||||||||||||||||||||||||||||||||||||||||
:text |
Select all unavailable elements |
Collection elements |
$(“:text”) Select all single lines of text |
||||||||||||||||||||||||||||||||||||||||||||||||
:password |
Select all password boxes |
Collection elements |
$(“: password”)Select all password boxes |
||||||||||||||||||||||||||||||||||||||||||||||||
:radio |
Select all radio boxes |
Collection elements |
$(“:radio”)Select all radio boxes |
||||||||||||||||||||||||||||||||||||||||||||||||
:checkbox |
Select all checkboxes |
Collection elements |
$(“:checkbox”)Select all checkboxes |
||||||||||||||||||||||||||||||||||||||||||||||||
:submit |
Select all submit buttons |
Collection elements |
$(“:submit”)Select all submit buttons |
||||||||||||||||||||||||||||||||||||||||||||||||
:image |
Select all images button |
Collection elements |
$(“:image”)Select all image buttons |
||||||||||||||||||||||||||||||||||||||||||||||||
:reset |
Select all reset buttons |
Collection elements |
$(“:reset”)Select all reset buttons |
||||||||||||||||||||||||||||||||||||||||||||||||
:button |
Select all buttons |
Collection elements |
$(“:button”)Select all buttons |
||||||||||||||||||||||||||||||||||||||||||||||||
:file |
Select all upload domains |
Collection elements |
$(:file)Select all upload domains |
||||||||||||||||||||||||||||||||||||||||||||||||
:hidden |
Select all invisible elements |
Collection elements |
$(“:hidden”)Select all invisible elements |

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

Dreamweaver CS6
Visual web development tools