使用jQuery选择器选择页面元素,目的是为了生成jQuery对象,语法相当简单:$(selector)。但值得注意的是,这是生成jQuery对象,不是DOM对象,因此$(selector).innerHTML以获取元素内部HTML代码是错误的,正确写法是$(selector).html()。同样的,判断一个DOM对象是否存在,不能够写成if($(selector)),而是if($(selector).length>0)。
当然jQuery对象和DOM对象可以互转换。从上面的例子也可以看出,jQuery对象可以视为是一个DOM对象数组,因此转换成DOM对象可使用get(index)方法或者[index]取下标;相反,DOM对象转换为jQuery对象只需直接用$(document.getElementById(“id”))包装一下就行了。
虽然取对象的方法很简单$(selector),但是这个参数selector却是种类繁多。这里扼要说明一下:
过滤选择器:附在所有选择器的后面,通过特定的过滤规则来筛选出一部分元素,如$(selector:first)。若单独使用,$(:first)则等价于$(*:first);
层次选择器:通过DOM元素间层次关系来获取特定元素,由两个选择器组合而成。选择过程为先按照第一个选择器选择元素,然后根据符号确定后代元素或子元素或兄弟元素,最后在这些元素范围内按照第二个选择器选取最后想要的元素;
下面就是各种选择器的表格说明- -这就打了我一上午,真蛋疼!
基本选择器
选择器 |
描述 |
返回 |
* |
选取所有元素 |
集合元素 |
element |
根据标签名选取元素 |
集合元素 |
#id |
根据id属性值选取元素 |
单个元素 |
.class |
根据class属性值选取元素 |
集合元素 |
selector1,selector2,…,selectorN |
将每个选择器选取的元素合并在一个结果,主要用于选取不同元素 |
集合元素 |
基本过滤选择器
选择器 |
描述 |
返回 |
:first |
选取第一个元素 |
单个元素 |
:last |
选取最后一个元素 |
单个元素 |
:even |
选取索引值是偶数的所有元素,索引从0开始 |
集合元素 |
:odd |
选取索引值是奇数的所有元素,索引从0开始 |
集合元素 |
:eq(index) |
选取索引值等于index的元素,index从0开始 |
单个元素 |
:gt(index) |
选取索引值大于index的元素,index从0开始 |
集合元素 |
:lt(index) |
选取索引值小于index的元素,index从0开始 |
集合元素 |
:not(selector) |
选取匹配selector以外的元素 |
集合元素 |
:header |
选取所有的标题元素 |
集合元素 |
:animated |
选取当前正在执行动画的所有元素 |
集合元素 |
子元素过滤选择器
选择器 |
描述 |
返回 |
:first-child |
选取每个父元素的第一个子元素 |
集合元素 |
:last-child |
选取每个父元素的最后一个子元素 |
集合元素 |
:only-child |
如果某元素是父元素唯一的子元素,则将被选取 |
集合元素 |
:nth-child(odd) |
选取每个父元素下索引值是奇数的子元素 |
集合元素 |
:nth-child(even) |
选取每个父元素下索引值是偶数的子元素 |
集合元素 |
:nth-child(index) |
选取每个父元素下索引值等于index的子元素 |
集合元素 |
:nth-child(equation) |
选取每个父元素下索引值匹配equation的子元素 |
集合元素 |
内容过滤选择器
选择器 |
描述 |
返回 |
:contains(text) |
选取文本内容为text的元素 |
集合元素 |
:has(selector) |
选取含有后代元素为selector的元素 |
集合元素 |
:parent |
选取含有后代元素或文本的元素 |
集合元素 |
:empty |
选取不包含后代元素或文本的空元素 |
集合元素 |
可见性过滤选择器
选择器 |
描述 |
返回 |
:hidden |
选取所有不可见的元素 |
集合元素 |
:visible |
选取所有可见的元素 |
集合元素 |
属性过滤选择器
选择器 |
描述 |
返回 |
[attr] |
选取拥有attr属性的元素 |
集合元素 |
[attr=value] |
选取attr属性值为value的元素 |
集合元素 |
[attr!=value] |
选取attr属性值不为value的元素 |
集合元素 |
[attr^=value] |
选取attr属性值以value开始的元素 |
集合元素 |
[attr$=value] |
选取attr属性值以value结束的元素 |
集合元素 |
[attr*=value] |
选取attr属性值含有value的元素 |
集合元素 |
[attr~=value] |
选取attr属性值用空格分隔的值中有一个为value的元素 |
集合元素 |
[selector1][selector2]…[selectorN] |
选取满足所有属性过滤选择器的元素 |
集合元素 |
层次选择器
选择器 |
描述 |
返回 |
selector1 selector2 |
从selector1的后代元素里选取selector2 |
集合元素 |
selector1>selector2 |
从selector1的子元素里选取selector2 |
集合元素 |
Selector1+selector2 |
从selector1后面的第一个兄弟元素里选取selector2 |
集合元素 |
selector1~selector2 |
从selector1后面的所有兄弟元素里选取selector2 |
集合元素 |
表单选择器
选择器 |
描述 |
返回 |
:input |
选取 |
集合元素 |
:text |
选取符合[type=text]的元素 |
集合元素 |
:password |
选取符合[type=password]的元素 |
集合元素 |
:radio |
选取符合[type=radio]的元素 |
集合元素 |
:checkbox |
选取符合[type=checkbox]的元素 |
集合元素 |
:image |
选取符合[type=image]的元素 |
集合元素 |
:file |
选取符合[type=file]的元素 |
集合元素 |
:button |
选取符合[type=button]的和 |
集合元素 |
:submit |
选取符合[type=submit]的 |
集合元素 |
:reset |
选取符合[type=reset]的 |
集合元素 |
:hidden |
选取所有不可见的元素 |
集合元素 |
Form filter selector
|
Description |
Return |
|||||||||||||||
:enable | Select all available form elements |
Collection elements |
|||||||||||||||
:disable | Select all unavailable form elements |
Collection elements |
|||||||||||||||
:checked | Select the selected element (radio button, check box) |
Collection elements |
|||||||||||||||
:selected | Select the selected |
Collection 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools