js中删除特定ID或特定class的元素
1、移除ID名为id_name的元素var x = document.getElementById("id_name");//如果对象x不为空if (x != null)x.remove();PS:参考链接 https://www.w3school.com.cn/htmldom/met_select_remove.asp对于selectObject.remove(index)方法,以select元
·
1、移除ID名为id_name的元素
var x = document.getElementById("id_name");
//如果对象x不为空
if (x != null)
x.remove();
PS:参考链接 https://www.w3school.com.cn/htmldom/met_select_remove.asp
对于selectObject.remove(index)方法,以select元素为例,
若指定index,则只删除其中的某一项;
若不指定index,则删除整个select元素。
2、移除Class名为class_name的元素,以移除div元素为例
$("div.class_name").remove();
若调试时发现类名中出现空格,则将空格替换为“.”执行代码,假设类名为“name1 name2”,则
$("div.name1.name2").remove();
其中,$()是jQuery中的一个函数,用于获取目标对象,使用前需要添加
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
以上。
更多推荐
已为社区贡献1条内容
所有评论(0)