我必须通过匹配条件来制作元素。但是当我执行另一个时,之前创建的元素会留在那里。如何使用java脚本避免这种重复? 我有这样的 Javascript 函数: function maketext(p) { var f = document.createElement('form'); f.setAttribute('method', "post"); f.setAttribute('id', "update"); f.setAttribute('action', "updateprod"); if (p == "Code") { var inp = document.createElement('input'); inp.setAttribute("placeholder", "Type Here"); inp.setAttribute("class", "form-control"); inp.setAttribute("id", "mcode"); inp.setAttribute("name", "mcode"); var sub = document.createElement("input"); sub.setAttribute("type", "submit"); sub.setAttribute("value", "submit"); sub.setAttribute("class", "btn btn-primary"); f.appendChild(inp); f.appendChild(sub); document.getElementById("mys").appendChild(f); } if (p == "Name") { var inp = document.createElement('input'); inp.setAttribute("placeholder", "Type Here"); inp.setAttribute("class", "form-control"); inp.setAttribute("id", "mname"); inp.setAttribute("name", "mname"); var sub = document.createElement("input"); sub.setAttribute("type", "submit"); sub.setAttribute("value", "submit"); sub.setAttribute("class", "btn btn-primary"); f.appendChild(inp); f.appendChild(sub); document.getElementById("mys").appendChild(f); }}html <a href="#" onclick="maketext('Code')" data-toggle="modal" data-target="#myModal" class="btn btn-info btn-lg">Edit</a><a href="#" onclick="maketext('Name')" data-toggle="modal" data-target="#myModal" class="btn btn-info btn-lg">Edit</a>
1 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
在创建元素之前删除所有以前的元素 div
function maketext(p) {
document.getElementById("mys").innerHTML = ''
// rest of the code
}
添加回答
举报
0/150
提交
取消