如何使用jQuery删除指定元素后跟的所有兄弟节点?

2026-03-31 14:461阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计651个文字,预计阅读时间需要3分钟。

如何使用jQuery删除指定元素后跟的所有兄弟节点?

删除方法:

1.使用$(selector).next().remove()语句删除指定元素后的第一个兄弟节点;

2.使用$(selector).nextAll().remove()语句删除指定元素后的所有兄弟节点。

操作环境:window

删除方法:1、使用“$(selector).next().remove()”语句删除指定元素后的第一个兄弟节点;2、使用“$(selector).nextAll().remove()”语句删除指定元素后的所有兄弟节点。

如何使用jQuery删除指定元素后跟的所有兄弟节点?

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

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

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

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

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

<!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>

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

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

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

<!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>

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

标签:兄弟节点

本文共计651个文字,预计阅读时间需要3分钟。

如何使用jQuery删除指定元素后跟的所有兄弟节点?

删除方法:

1.使用$(selector).next().remove()语句删除指定元素后的第一个兄弟节点;

2.使用$(selector).nextAll().remove()语句删除指定元素后的所有兄弟节点。

操作环境:window

删除方法:1、使用“$(selector).next().remove()”语句删除指定元素后的第一个兄弟节点;2、使用“$(selector).nextAll().remove()”语句删除指定元素后的所有兄弟节点。

如何使用jQuery删除指定元素后跟的所有兄弟节点?

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

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

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

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

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

<!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>

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

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

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

<!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>

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

标签:兄弟节点