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

How to get the parent node in jquery

王林
王林Original
2020-11-25 11:39:392583browse

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)

Parameters:

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

Example:


Return the direct parent element of 45a2772a6b6107b401db3c9b82c049c2:

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

Output result:

body (great-great-grandparent)div (great-grandparent)
ul (grandparent)li (direct parent) span

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!

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