将div内容替换为javascript

eagi6jfj  于 2023-06-04  发布在  Java
关注(0)|答案(7)|浏览(141)

我试着用纯javascript来做这件事,而不是用jquery。我有一个id为test的div,里面包含其他div。我如何清空这个div的内容并将其替换为其他html?

<div id="test">
   <div>...</div>
   <div>...</div>
   <div>...</div>
</div>
vmpqdwk3

vmpqdwk31#

document.getElementById("test").innerHTML = "new content"
uplii1fm

uplii1fm2#

清除div:

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

替换它:

var h1 = document.createElement('h1');
h1.innerHTML = "hello world!";
document.getElementById('test').appendChild(h1);
tcbh2hod

tcbh2hod3#

使用document.getElementById('test').innerHTML = ''

xzv2uavs

xzv2uavs4#

在我的头顶上,我认为这应该起作用......

document.getElementById("test").innerHTML = "";
oyxsuwqo

oyxsuwqo5#

document.getElementById("test").innerHTML = "Some other Content";
yhuiod9q

yhuiod9q6#

var testDiv = document.getElementById("test");
testDiv.innerHTML = "<h5>It works!</h5>";

相关问题