jquery 查找祖先元素

In this post, we will discuss about the traversal method to get the first ancestor of the selected HTML element. jQuery API provides closest() method to perform this task.

在本文中,我们将讨论有关遍历方法以获取所选HTML元素的第一个祖先的信息。 jQuery API提供了closest()方法来执行此任务。

jQuery最接近的() (jQuery closest())

The jQuery closest() method returns the first matched ancestor element of the selected HTML element. The method accepts a mandatory parameter, which filters out the traversal. This method starts traversal by first looking at the current element, it will return the current element if it matches the specified expression else it will traverse up the DOM tree until it found an element which matches the filter. This method will not return anything if the search does not found any matching element.

jQuery最接近的()方法返回所选HTML元素的第一个匹配的祖先元素。 该方法接受一个强制性参数,该参数会过滤掉遍历。 此方法首先通过查看当前元素开始遍历,如果它与指定的表达式匹配,它将返回当前元素,否则它将遍历DOM树直到找到与过滤器匹配的元素。 如果搜索未找到任何匹配的元素,则此方法将不返回任何内容。

Here is the general syntax for using closest() method:

以下是使用最近的()方法的常规语法:

  • selector.closest(filter)selector.closest(filter)
  • selector.closest(filter,context)selector.closest(filter,context)

filter is a mandatory parameter passed to this method which filter out the ancestor search.

filter是传递给此方法的必需参数,可过滤出祖先搜索。

context is an optional parameter, which is a DOM element. A matching element may be found within this context.

context是一个可选参数,它是一个DOM元素。 在此上下文中可以找到匹配的元素。

jQuery最近的()函数示例 (jQuery closest() function example)

Following example demonstrates the jQuery closet() method usage.

以下示例演示了jQuery closet()方法的用法。

<!doctype html>
<html>
<head>
<title>jQuery Traversing closest</title>
<style>
div,span{display: block;border: 3px solid lightgrey;color: black;padding: 5px;margin: 25px;width:300px;}
span.highlight {
background: yellow;
}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<div><span><b>Click me!</b></span><span><b>Click me!</b></span><span>You can also <b>Click me!</b></span>
</div>
<script>
$( document ).on( "click", function( event ) {
$( event.target ).closest( "span" ).toggleClass( "highlight" );
});
</script>
</body>
</html>

In this example, there are three span elements within the div element. The closest() method triggers when you click on the span elements. The filter passed here is “span” and it will first check the current element is a span. Since we click on the span element, the method will return that element and we carry out the desired actions. Here we use jQuery toggleClass to toggle between the CSS style class to change the background color.

在此示例中,div元素内有三个span元素。 单击span元素时,最接近的()方法触发。 此处通过的过滤器为“ span”,它将首先检查当前元素是否为span。 由于单击span元素,因此该方法将返回该元素,并执行所需的操作。 在这里,我们使用jQuery toggleClass在CSS样式类之间切换以更改背景颜色。

This method will traverse up the DOM tree if the matching element is not found. You can try it out by clicking in any of the boxes below.

如果找不到匹配的元素,则此方法将遍历DOM树。 您可以通过点击下面的任何框来尝试一下。

演示地址

That’s all for now. we will look into more jQuery traversing methods in coming posts.

目前为止就这样了。 我们将在以后的文章中研究更多的jQuery遍历方法。

翻译自: https://www.journaldev.com/5321/get-first-ancestor-element-jquery

jquery 查找祖先元素

jquery 查找祖先元素_如何获得jQuery中元素的第一个祖先相关推荐

  1. jquery获得当前元素父级元素_如何使用jQuery获取父元素

    jQuery获取父元素我们有三种方式可以实现,.parent()..parents()..closest(),下面我们将介绍jQuery获取父元素的这三种方式以及一个具体的示例.web前端学习:打造全 ...

  2. jquery 获取同级元素_如何在jQuery中获取元素的同级

    jquery 获取同级元素 In this post, we will discuss how to get the siblings of an HTML element in jQuery. jQ ...

  3. jquery项目源码_第一个jQuery程序

    1.配置jQuery环境 1.1获取jQuery最新版本 进入jQuery的官方网址 http://jquery.com ,下载最新的jQuery库. 1.2 jQuery库类型说明 目前jQuery ...

  4. jquery前端简单分页_如何使用jQuery创建简单的分页

    jquery前端简单分页 How to create easy pagination with jQuery jQuery pagination. All we know, that when we ...

  5. java 查找链表中间元素_如何在Java中一次性查找Java中链表的中间元素

    如何在一次传递中找到LinkedList的中间元素?这是一个 Java 和非Java程序员面试时经常被问到的编程问题.这个问题类似于检查回文或计算阶乘,有时也会要求编写代码.为了回答这个问题,候选人必 ...

  6. js数组查找最接近_在JavaScript数组中找到最小元素的位置

    在JavaScript数组中找到最小元素的位置 注*  之前有篇文章介绍过数据遍历的性能比较: for in 比for loop慢至少20倍 ,这是另外一篇比较数组查找性能的例子,通过对手工/inde ...

  7. python取出矩阵中的某一元素_将tensorflow.Variable中的某些元素取出组成一个新的矩阵示例...

    在神经网络计算过程中,经常会遇到需要将矩阵中的某些元素取出并且单独进行计算的步骤(例如MLE,Attention等操作).那么在 tensorflow 的 Variable 类型中如何做到这一点呢? ...

  8. java数组删除数组元素_如何在Java中删除数组元素

    java数组删除数组元素 When we create an array in Java, we specify its data type and size. This is used by JVM ...

  9. 地壳中元素含量排名记忆口诀_地壳中含量最多的元素是什么?地壳中元素含量排名口诀...

    地壳,乃地质术语,是指由岩石组成的固体地壳,是地球固体圈的最外层,也是岩石圈的重要组成部分.那么地壳中含量最多的元素是什么呢?以及地壳中元素含量排名口诀又是什么呢? 地壳中含量最多的元素 从地震波的研 ...

最新文章

  1. c# image转换为bitmap_C# 人脸识别库
  2. 如何学习数据结构和算法——大佬文章汇总
  3. 动态创建Fastreport
  4. sql转化为int类型
  5. Gradle项目同步失败错误
  6. 基于jquery的ajax分页效果
  7. 李瑾博士:信誉的建立是否“不计成本”?
  8. JMeter入门合集
  9. C语言文件单字符读写,c语言文件读写操作
  10. ElasticSearch服务器的搭建与使用
  11. matlab实现隐函数求偏导数(impldiff函数)
  12. markdown编辑器和富文本编辑器区别
  13. CuPy is not correctly installed
  14. C语言100个囚犯和灯泡,一百个囚犯和一个灯泡
  15. OpenMP Sections
  16. BZOJ 2901: 矩阵求和
  17. vue---父组件实时取到子元素的v-model
  18. 【动态规划】区间动态规划
  19. FC按键修改教程之一键开关
  20. tp5+mysql写入特殊表情符号数据时失败

热门文章

  1. 连接Oracle9i,因字符集造成乱码的解决方法
  2. Oracle 11g完全卸载(Windows)
  3. 如何在繁忙的工作中锻炼身体?
  4. [转载] 递归函数python基例_python递归函数详解 python 递归函数使用装饰器
  5. [转载] python匿名(lambda)函数
  6. [转载] Python字符串常用操作命令
  7. [转载] Java构造方法、重载和重写
  8. Minimum Window Substring @LeetCode
  9. 为什么公司宁愿 25K 重新招人,也不给你加到 20K?原因太现实……
  10. python--列表,元组,字符串互相转换