function AddButton() {
var myDiv =document.getElementById("divItem").parentNode;
var newButton =document.createElement("button");
newButton.innerHTML = "新按钮";
newButton.onclick = function () {
// 绑定单击事件
};
// myDiv.appendChild(newButton); // 在后面添加
// 方法一:利用jquery方法
// myDiv.id = "mydiv";
// $("#mydiv").prepend(newButton);
// 方法二: 利用html的js方法
myDiv.insertBefore(newButton, myDiv.firstChild);
}
AddButton();
js/jquery动态在div的开头插入button
最新推荐文章于 2022-09-21 13:35:19 发布