加入收藏 | 设为首页 | 会员中心 | 我要投稿 烟台站长网 (https://www.0535zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

jQuery中清除兄弟元素的方法是什么,代码如何写?

发布时间:2022-02-08 14:22:44 所属栏目:语言 来源:互联网
导读:jQuery中清除兄弟元素的方法是什么?在jQuery中,我们想要清楚兄弟元素,可以使用到remove()方法,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。 本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。 jq
      jQuery中清除兄弟元素的方法是什么?在jQuery中,我们想要清楚兄弟元素,可以使用到remove()方法,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
 
      本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。
 
      jquery清除兄弟元素的方法
 
     使用siblings()方法获取被选元素的所有兄弟元素
 
     使用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").siblings().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>

(编辑:烟台站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读