简体   繁体   English

如何通过使用Java循环来删除所有HTML标记

[英]How to remove all HTML tag by using the loop with Javascript

<div id='images'><img id='center_loadingImage' align='middle' src='loading.gif' alt='Loading Image'></div>

How to remove all <div> with the loop with Javascript? 如何使用Java循环删除所有<div>

图片

Here is my code: 这是我的代码:

var value = document.getElementsByTagName("images");
for (var i = 0; i < value.length; i++) {
    $(value[i]).remove();
}

You can only use same id value once per page. 每页只能使用相同的ID值一次。 Change it to class, ie images 将其更改为类,即images

You will then have multiple div with class images and will be able to easily remove the spinners like this: 然后,您将获得带有类images多个div ,并将能够轻松地移除微调器,如下所示:

$(".images").remove();

If you have a lot of spinners, just wrap them with a div and remove the div. 如果您有很多微调框,只需将它们用div包装,然后移除div。 Something like this: 像这样:

HTML: HTML:

<div id="jedi-wrapper">
    <div class="images">
        ...
    </div>
</div>

jQuery: jQuery的:

$("#jedi-wrapper").remove();

From the image, it looks like you are loading some values using AJAX. 从图像看来,您正在使用AJAX加载一些值。 Why don't you remove the image on success? 为什么不成功删除图像?

Hope that helps 希望能有所帮助

Seem like you want to remove all div with id images, but id is unique, you can use class instead: 似乎您想删除所有带有id图像的div ,但是id是唯一的,您可以改用class:

<div class='images'><img class='center_loadingImage' align='middle' src='loading.gif' alt='Loading Image'></div>

then you can do: 那么您可以执行以下操作:

$('.images').remove()

With your code you can do this: 使用您的代码,您可以执行以下操作:

document.getElementsByClassName("images").remove();

or more like jQuery: 或更像jQuery:

$('.images').remove();

Althoug you can try this too: Althoug您也可以尝试以下方法:

var value = document.getElementsByClassName("images");
for (var i = 0; i < value.length; i++) {
   $(value).eq(i).remove();
} //-------^^^^^^--------------you can make use of `.eq()` here

What your issue is there is no tag name like 'images' as your var suggests. 您的问题是,没有var建议的像'images'这样'images'标签名称。

var value = document.getElementsByTagName("images");

images is the class name so you can use this: images是类名,因此您可以使用以下名称:

 document.getElementsByClassName("images")

Get element by ID, there's nothing with document.getElementsByTagName("images") 通过ID获取元素,document.getElementsByTagName(“ images”)没有任何内容

var c = document.getElementById('images');
var i, item = c.childNodes;
for (i = item.length; i--;) {
    c.removeChild(item[i]);
}

You should probably be using class="images" instead of id="images" if that element is being rendered multiple times. 如果该元素被多次渲染,则可能应该使用class="images"而不是id="images"

But to do this in a loop with raw javascript, you will need to first get the elements, convert them to an array, and then remove them in a loop. 但是要使用原始javascript在循环中执行此操作,您需要首先获取元素,将其转换为数组,然后在循环中将其删除。

var imageElements = doc.getElementsByClassName('images'),
    images = Array.prototype.slice.call(imageElements);

for (var i = 0, l = images.length; i < l; i++) {
    images[i].remove();
}

Notice that I don't just loop through imageElements ... That's because getElementsBy...() returns a live list , so as soon as you remove() one of them, the list will be mutated and you will start running into undefined elements and javascript errors. 请注意,我不只是遍历imageElements ……这是因为getElementsBy...()返回一个实时列表 ,因此,一旦您remove()其中的一个,列表将被更改,并且您将开始运行到undefined元素和javascript错误。 To solve this, simply convert the live list to an array with Array.prototype.slice.call() and then loop through that array, removing the elements from the page. 要解决此问题,只需使用Array.prototype.slice.call()将活动列表转换为数组,然后遍历该数组,从页面中删除元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM