Home > Web Front-end > JS Tutorial > How to get the parent node in jquery

How to get the parent node in jquery

王林
Release: 2020-11-30 14:26:07
Original
2586 people have browsed it

Jquery method to obtain the parent node: It can be obtained through the parent method, such as [$(selector).parent(filter)]. The parent method can return the direct parent element of the selected element.

How to get the parent node in jquery

The operating environment of this tutorial: Windows 10 system, jquery version 2.2.4. This method is suitable for all brands of computers.

(Learning video sharing: jquery video tutorial)

Method introduction:

parent() method returns the direct parent element of the selected element.

Syntax:

$(selector).parent(filter)
Copy after login

Parameters:

  • ##filter Optional. Specifies a selector expression that narrows the search for parent elements.

Example:


Return the direct parent element of :

$(document).ready(function(){
$("span").parent().css({"color":"red","border":"2px solid red"});
});
Copy after login

Output result:

body (great-great-grandparent)div (great-grandparent)
ul (grandparent)li (direct parent) span
Copy after login
Related recommendations:

js tutorial

The above is the detailed content of How to get the parent node in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template