按类名删除元素? [英] Removing elements by class name?
本文介绍了按类名删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码来查找带有类名的元素:
I have the below code to find elements with their class name:
// Get the element by their class name
var cur_columns = document.getElementsByClassName('column');
// Now remove them
for (var i = 0; i < cur_columns.length; i++) {
}
我只是不知道如何删除它们......我是否必须引用父母或其他东西?处理此问题的最佳方法是什么?
I just don't know how to remove them..... do I HAVE to reference the parent or something? What's the best way to handle this?
@ Karim79:
这是JS:
var col_wrapper = document.getElementById("columns").getElementsByTagName("div");
var len = col_wrapper.length;
alert(len);
for (var i = 0; i < len; i++) {
if (col_wrapper[i].className.toLowerCase() == "column") {
col_wrapper[i].parentNode.removeChild(col_wrapper[i]);
}
}
这是HTML:
<div class="columns" id="columns">
<div class="column"><input type="checkbox" name="col_list[]" value="cows">cows</div>
<div class="column"><input type="checkbox" name="col_list[]" value="cows">cows</div>
<div class="column"><input type="checkbox" name="col_list[]" value="cows">cows</div>
<div class="column"><input type="checkbox" name="col_list[]" value="cows">cows</div>
<div name="columnClear" class="contentClear" id="columnClear"></div>
</div>
编辑:最后只使用jQuery选项。
Well ended up just using the jQuery option.
推荐答案
使用jQuery(我认为你可以在这种情况下使用它),你可以这样做:
using jQuery (which you really could be using in this case, I think), you could do this like so:
$('.column').remove();
否则,您将需要使用每个元素的父元素将其删除:
Otherwise, you're going to need to use the parent of each element to remove it:
element.parentNode.removeChild(element);
这篇关于按类名删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文