


Tutorial on the difference between parent(), parents() and parentsUntil() in JQuery and how to use it
parent() is actually very simple, it just specifies the outer layer of each element in the selector. For example, $("p"), then the result is data wrapped in
, such as: . If there are multiple, the result set is a object set. You can use eq(i), first(), last() to locate. If the data is
-
how to use $("p").parent("li") the query result is empty because the outer layer of
is < ;p>.
JQueryIn the manual,
<p></p><p>Hello</p><p></p><p>Hello Again</p>
<pre class="brush:php;toolbar:false">$("p").parent(".selected");
//结果:
[ <p></p><p>Hello Again</p> ]
can be understood as
$("p").parent().find(".selected");
Find the one or more of the direct parent whose class is selected. indivual.
So parent() is not very practical.
parents() is all the parents of each element in the selector. If the selector has multiple parents, it will return a result set. The results of the result set are arranged from the inner layer to the outer layer. The outermost layer is the entire html element, and the first one is the element that tightly wraps each element in the selector. There are no duplicate elements in the result set.
Similarly, when parameters are taken, the specified element is also found in the result set
$("p").parents(".selected");
$("p").parents().find(".selected");
So if we want to find a certain father of the selector, we must first find all his parents, and then find This father.
When we want to find a parent of the current element, the following syntax is used.
$(this).parents("li");//$(this).parents().find("li");
So parents() is very practical. Very commonly used.
parentsUntil(expr). If parents() is to find all the parents of each element in the selector in the entire html element, then the function of parentsUntil() is to limit the scope of the search. This range is $(expr).html()
$("p").parents();//包含整个html 元素$("p").parentsUntil("html");//不包含整个html元素
The result sets of the above two expressions only differ by one element, which is the entire html element.
$("p").parents();//包含整个html 元素$("p").parentsUntil();//包含整个html元素
When parentsUntil() takes no parameters, it is equivalent to parents();
The results in the result set of parentsUntil(expr) are also arranged from the inner layer to the outer layer. parentUntil(expr) will search outward layer by layer starting from the first parent element that wraps the element until it encounters the first element that matches the expression (expr).
$("p").parentsUntil(expr);
Equivalent to searching all the parent elements of each element in $("p") from the inside and outside of $(expr)
In order to find the specified element more accurately , can be used as follows.
$("p").parentsUntil("ul").find("li");
But the above method cannot find ul>li, only ul * li. Because parentsUntil() returns the largest parent in the result set is ul>* and find is to find elements that do not include ul>* in the descendant elements of ul>*. Therefore, it is not feasible to use this method to find ul>li
You can use the following code to query all li under ul (first line), and all li under the nearest ul (second line)
$("p").parents("ul").find("li");$("p").parents("ul").eq(0).find("li");
parentsUntil(expr), the returned result set does not include expr itself, and if find() is used later, it will be queried in the descendant elements of the result set, so the query result is not the child elements of expr but the second generation and Child elements after the second generation.
In view of the performance of parentsUntil, it is generally not recommended to use it.
parnetsUntil() can be used to find certain elements under a certain module.
When we want to specifically operate on an element in the result set, we must remember to use eq(i), first(), last() and other functions to obtain the value, otherwise the operation will be the entire result set.
Dynamic search for parent nodes
$(this).parent("li");//找到第一个包住$(this)的dom,如这个dom是li则返回他的对象,如果不是则返回空对象$(this).parents("li");//找到所有$(this)的父亲,并在其中找出所有的li的对象,组成结果集。结果集中结果由内之外排列$(this).parents().find("li");//同上。$(this).parents("li").eq(0);//$(this)外第一个包裹他的li对象$(this).parentsUntil("li");//$(this)外到第一个包裹他的li之内的所有的$(this)的父亲$(this).parentsUntil("ul").find("li");//$(this)在ul之前的那个父亲之内(不包括该父亲)找所有的li;如果
The above is the detailed content of Tutorial on the difference between parent(), parents() and parentsUntil() in JQuery and how to use it. For more information, please follow other related articles on the PHP Chinese website!

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

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.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

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

Atom editor mac version download
The most popular open source editor
