Home > Web Front-end > JS Tutorial > body text

How to find the parent class of a specified element in jquery

coldplay.xixi
Release: 2023-01-04 09:38:04
Original
5124 people have browsed it

Jquery method to find the parent class of a specified element: 1. Use the [closest()] method to return the first ancestor of the selected element; 2. Use the [parent()] method to return the selected element The element's immediate parent.

How to find the parent class of a specified element in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, DELL G3 computer.

Recommended: jquery video tutorial

##jquery method to find the parent class of a specified element:

In jquery, you can use the closest() method and parent() method to find the parent class of a specified element.

  • closest() method is used to return the first ancestor of the selected element.

  • parent() method is used to return the direct parent element of the selected element.

Example 1: Use the closest() method to get the first matching ancestor of an element.






点击按钮,查看结果

Copy after login

Rendering:

How to find the parent class of a specified element in jquery

Example 2: Use the parent() method to get the direct parent element of the element









body (曾曾祖父节点)
div (曾祖父节点)
    ul (祖父节点)
  • li (直接父节点) span
  • li (直接父节点) span
Copy after login

Rendering:

How to find the parent class of a specified element in jquery

Related free learning recommendations: javascript(Video)

The above is the detailed content of How to find the parent class of a specified element 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!