我是靠谱客的博主 细腻睫毛,这篇文章主要介绍jquery怎么删除指定元素后的兄弟节点,现在分享给大家,希望可以做个参考。

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。

jquery删除指定元素后的兄弟节点的方法

1、利用next()和remove()方法

  • 使用next()获取被选元素的下一个兄弟节点。

  • 使用remove()方法删除获取到的兄弟元素

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> .siblings,.siblings *{ display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("button").click(function() { $("h2").next().remove(); }); }); </script> </head> <body> <div class="siblings">div (父) <p>p(兄弟元素)</p> <span>span(兄弟元素)</span> <h2>h2(本元素)</h2> <h3>h3(兄弟元素)</h3> <p>p(兄弟元素)</p> </div> <button>清除兄弟元素</button> </body> </html>
登录后复制

1.gif

2、利用nextAll()和remove()方法

  • 使用next()获取被选元素的所有兄弟节点。

  • 使用remove()方法删除获取到的兄弟元素

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> .siblings,.siblings *{ display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("button").click(function() { $("h2").nextAll().remove(); }); }); </script> </head> <body> <div class="siblings">div (父) <p>p(兄弟元素)</p> <span>span(兄弟元素)</span> <h2>h2(本元素)</h2> <h3>h3(兄弟元素)</h3> <p>p(兄弟元素)</p> </div> <button>清除兄弟元素</button> </body> </html>
登录后复制

2.gif

相关视频教程推荐:jQuery教程(视频)

以上就是jquery怎么删除指定元素后的兄弟节点的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是细腻睫毛最近收集整理的关于jquery怎么删除指定元素后的兄弟节点的全部内容,更多相关jquery怎么删除指定元素后内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部