简体   繁体   English

如何在DOM中“刷新”元素?

[英]How do I “refresh” element in DOM?

I've got an empty DIV element in which I append images by using function createElement("img") and append them with appendChild. 我有一个空的DIV元素,我在其中使用函数createElement(“img”)附加图像,并使用appendChild附加它们。 So now I've got DIV element full of images. 所以现在我已经拥有了充满图像的DIV元素。

I would like to use one button to clean this DIV and add new images in it simultaneously. 我想用一个按钮来清理这个DIV并同时在其中添加新图像。 Thanks for your help 谢谢你的帮助

Are you just looking for method replaceChild? Or you could remove all child elements before adding new images: 你只是在寻找方法replaceChild? Or you could remove all child elements before adding new images: replaceChild? Or you could remove all child elements before adding new images:

// assuming yor div is in variable divelement
while (divelement.firstChild)
  divelement.removeChild(divelement.firstChild);

what do you mean by clean? 干净是什么意思? If you just want to empty it, you can do 如果你只是想清空它,你可以这样做

document.getElementById('mydiv').innerHTML = '';

And then add on whatever new images you want. 然后添加你想要的任何新图像。

虽然在循环中设置innerHTML和调用removeChild()都会清除DIV中的内容,但由于当今浏览器的性质,innerHTML方法会更快。

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

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