我有一个带有以下 id="loader-wrap" 的元素
<div id="loader-wrap"></div>
我想在上面的父 div 上自动添加一个新的 div 元素,包括在我有这个 Id 元素的地方添加类“Pin”。
想要这个: <div id="loader-wrap"><div class="Pin"></div></div>
我有一个带有以下 id="loader-wrap" 的元素
<div id="loader-wrap"></div>
我想在上面的父 div 上自动添加一个新的 div 元素,包括在我有这个 Id 元素的地方添加类“Pin”。
想要这个: <div id="loader-wrap"><div class="Pin"></div></div>
使用 document.createElement() 方法:
var pin = document.createElement("div"); // create new div
pin.setAttribute("class", "Pin"); // set class to the div
var loaderWrap = document.getElementById("loader-wrap"); // get the parent element
loaderWrap.appendChild(pin); // append the new div to the parent