js动态往div里添加按钮的两种方式

方式1:
var MyDiv =document.getElementById("div_id");
var button = document.createElement("input");
button.setAttribute("type", "button");
button.setAttribute("value", value);
button.setAttribute("id", id);
button.setAttribute("class", class);
button.style.width = "12%";
button.setAttribute("onclick", "function(this.id)");
MyDiv.appendChild(button);


方式2:
var button = "<a role='button' id="+jinfo[i].id+" onclick='function(this.id)'><i class='icon-edit'></i></a>";
MyDiv.innerHTML += button;


//第二种方式方便往<a></a>标签之间添加内容

posted @ 2018-05-04 16:23  晴天fly  阅读(22715)  评论(0编辑  收藏  举报