Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript类删除所有元素_Javascript_Html - Fatal编程技术网

使用javascript类删除所有元素

使用javascript类删除所有元素,javascript,html,Javascript,Html,我需要删除某个类中的所有元素。 我搜索并尝试了大多数选项,但没有在IE11上运行,我知道IE不支持remove()本机Javascript函数,但支持removeChild()。 使用removeChild()我得到以下消息:“对象不支持属性或方法‘removeChild’” html: 一些文本 一些文本 一些文本 一些文本 一些文本 一些文本 一些文本 一些文本 一些文本 使用此polyfill (function (arr) { arr.forEach(function (item

我需要删除某个类中的所有元素。 我搜索并尝试了大多数选项,但没有在IE11上运行,我知道IE不支持remove()本机Javascript函数,但支持removeChild()。 使用removeChild()我得到以下消息:“对象不支持属性或方法‘removeChild’”

html:


一些文本
一些文本
一些文本
一些文本
一些文本
一些文本
一些文本
一些文本
一些文本
使用此polyfill

(function (arr) {
  arr.forEach(function (item) {
    if (item.hasOwnProperty('remove')) {
      return;
    }
    Object.defineProperty(item, 'remove', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function remove() {
        this.parentNode.removeChild(this);
      }
    });
  });
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
更多详细信息和

首先,您在html类中缺少“=”。。 因此,在这个例子中,这应该是可行的:

函数removeElt(){
var elements=document.getElementsByClassName(“contentHolder”);
while(elements.length>0){
元素[0].parentNode.removeChild(元素[0]);
}
}

一些文本
一些文本
一些文本
一些文本
一些文本
一些文本
一些文本
一些文本
一些文本

使用
document.querySelectorAll(“.contentInfo”)获取类的所有元素然后循环它们以删除每个:

var elems=document.querySelectorAll(“.contentInfo”);
[]forEach.call(元素,函数(元素){
el.移除();
});

内容信息
contentTxt
contentTxt2
内容信息
contentTxt
contentTxt2
内容信息
contentTxt
contentTxt2
两个问题:

  • 您的HTML标记在此处缺少一个
    =

    <div class"contentHolder">
    
    
    一些文本
    一些文本
    一些文本
    一些文本
    一些文本
    一些文本
    一些文本
    一些文本
    一些文本
    
    删除内容
    可能重复请注意,for循环版本保留了其中一个要删除的元素,无论出于何种原因(由于Leo.R,代码中缺少了
    =
    ),我认为您忘记了for循环中的一个元素。循环运行和停止的条件是什么?@LeoR,条件部分用于减小
    i
    的值,因此不使用最后一部分(通常用于此类减小)。当前条件检查
    i
    的值,然后再将其递减。如果减量发生在最后一部分,这是不可能的。当然,有很多方法可以让反向循环工作。这是我找到并尝试过的第一个答案,但没有成功。我认为我犯了一个错误,现在尝试与原来的同时,工作完美。谢谢