jquery 获取同级元素

In this post, we will discuss how to get the siblings of an HTML element in jQuery. jQuery API provides siblings() method to achieve this functionality.

在本文中,我们将讨论如何在jQuery中获取HTML元素的同级元素。 jQuery API提供了siblings()方法来实现此功能。

jQuery兄弟姐妹() (jQuery siblings())

The siblings() method is used to return all the siblings of the selected HTML element. Unlike jQuery next() and prev() methods, this method traverses both forward and backwards along the siblings of the selected element. This method is very handy when you want to carry out similar tasks on a set of elements.

siblings()方法用于返回所选HTML元素的所有同级对象。 与jQuery next()prev()方法不同,此方法沿选定元素的同级对象向前和向后遍历。 当您要对一组元素执行类似任务时,此方法非常方便。

Here is the general syntax for using siblings method:

这是使用兄弟姐妹方法的一般语法:

  • selector.siblings( [filter ] )selector.siblings([ 过滤器 ])

filter is an optional parameter passed to the method to narrow down the traversal.

filter是传递给该方法的可选参数,以缩小遍历范围。

jQuery siblings()示例 (jQuery siblings() example)

Following example demonstrates the jQuery siblings() usage.

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

<!doctype html>
<html>
<head>
<title>jQuery Traversing siblings</title>
<style>span{color: blue;margin: 30px;font-size: 16px;padding: 5px;font-weight: bolder;border: 1px solid lightgrey;}.highlight{background: yellow;}div{ display: block;border: 3px solid lightgrey;color: lightgrey;padding: 5px;margin: 25px;width:350px;}
</style><script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<div><span> Bheem</span><span class="highlight"> Arjun</span><span> Nakul</span>
</div>
<div><span> Mark</span><span class="highlight"> Tom </span><span> Steve</span>
</div>
<div><span> Sachin</span><span> Saurav</span><span> Zaheer </span>
</div>
<script>$( ".highlight" ).siblings()
.css( "color", "red" );
</script>
</body>
</html>

In this example, you can see three div elements and each div has three span elements. In the first and second div elements, the second span has a CSS style class “highlight”. The siblings() method returns all the siblings of the selected element having class=”highlight” and changes the color of the siblings to red.

在此示例中,您可以看到三个div元素,每个div具有三个span元素。 在第一个和第二个div元素中,第二个跨度具有CSS样式类“ highlight” 。 siblings()方法返回具有class =“ highlight”的所选元素的所有同级并将同级颜色更改为红色。

In the firstdiv, Bheem and Nakul are siblings of Arjun and in the second div, Mark and Steve are siblings of Tom. This is how we get the siblings of an element in jQuery. Below image is the output produced by the above HTML page.

在第一个div ,Bheem纳库尔阿琼的兄弟姐妹,在第二div马克史蒂夫汤姆的兄弟姐妹。 这就是我们获取jQuery中元素的同级的方式。 下图是以上HTML页面产生的输出。

翻译自: https://www.journaldev.com/5301/how-to-get-the-siblings-of-an-element-in-jquery

jquery 获取同级元素

jquery 获取同级元素_如何在jQuery中获取元素的同级相关推荐

  1. python获取当前时间戳_如何在Python中获取当前时间戳?

    在Python中可以使用来自模块time.datetime或calendar的函数来获取当前时间戳,代码语句如[import time;ts = time.time() print(ts)]. 在Py ...

  2. python 获取cpu使用率_如何在python中获取过去10分钟的cpu使用率

    要测量CPU使用率,您需要比较两个给定时间的使用情况:您无法从过去获得度量点(除非您存储它,如@ajsp建议的那样).在 例如:import psutil import time def calcul ...

  3. java+script+当前日期_如何在JavaScript中获取当前日期?

    如何在JavaScript中获取当前日期? #1楼 您可以使用扩展了 Date对象的Date.js库,从而可以使用.today()方法. #2楼 如果您想对日期格式进行更多的粒度控制,我强烈建议您查看 ...

  4. php mysql 到表最后_如何在PHP中获取MySQL表的最后插入ID?

    如何在PHP中获取MySQL表的最后插入ID? 我有一张表,经常插入新数据. 我需要获取表格的最后一个ID. 我怎样才能做到这一点? 它类似于SELECT MAX(id) FROM table吗? g ...

  5. java文件中获取创建日期_如何在Java中获取文件的上次修改日期

    java文件中获取创建日期 Sometimes we need to get the file last modified date in Java, usually for listeners li ...

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

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

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

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

  8. python中获取文件大小_如何在Python中获取文件大小

    python中获取文件大小 We can get file size in Python using the os module. 我们可以使用os模块在Python中获取文件大小. Python中的 ...

  9. python 获取唯一值_从Python列表中获取唯一值

    python 获取唯一值 In this article, we will be understanding 3 ways to get unique values from a Python lis ...

最新文章

  1. 文字加减前后缀lisp_华为笔试题---仿LISP算法
  2. 快速设置UITableView不同section对应于不同种类的cell
  3. 一分钟了解阿里云产品:容器服务概述
  4. hdu6375(2018 “百度之星”程序设计大赛 - 初赛(A))
  5. ORA-12541:TNS没有监听器
  6. 类实现Java模板方法模式中的HookMethod实现
  7. SpringMVC Root WebApplicationContext启动流程
  8. Android的启动过程
  9. 关于Android的应用程序的发布的学习(一)
  10. CSS 背景尺寸 background-size属性
  11. html表单的课后心得体会,web前端学习心得体会范文
  12. python模拟浏览器下载文件_python第一个脚本,模拟浏览器下载文件 | 学步园
  13. BUAA_OO_第一次作业总结
  14. mongodb dbref java_Spring DATA MongoDB @DBref查询,or和and联合查询
  15. Java TemplateProcessingException之Cannot execute subtraction: operands are null and #1234
  16. 如何从课堂派上扒取直播视频
  17. 仿写天猫tmall首页
  18. FTP服务器搭建报错Warning: FTP over TLS is not enabled, users cannot securely log in.
  19. Java工作流管理系统(activity6.0)
  20. statuml怎么添加用户_starUML用户使用手册

热门文章

  1. SIM300命令参考
  2. 使用特殊字体实现特殊报表效果
  3. [转载] python截取指定字符串_python字符串截取,python字符串切片的方法详解
  4. [转载] Python面向对象编程系列第一篇
  5. [转载] Java默认构造方法
  6. jQuery读取和设定KindEditor值的方法
  7. 微软Azure云计算服务主导全球
  8. 最长不下降子序列的长度
  9. opencv-contrib-Python编译module ‘cv2.cv2‘ has no attribute ‘xfeatures2d‘
  10. 数据结构笔记(三十三)--二叉排序树的插入与生成