如何删除动态生成的特定输入文本?我尝试使用 remove() 但不工作。任何想法?
var txtLoop = 1;
function add(type) {
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", "text");
element.setAttribute("value", "typhere");
element.setAttribute("name", "txtbox" +txtLoop);
txtLoop++;
var btns = document.createElement("input");
btns.setAttribute("type", "button" );
btns.setAttribute("value", "delete");
btns.setAttribute("name", "dlete");
//This part is wrong, hmmm..
btns.setAttribute("onclick", var elem = document.getElementById("txtbox");
elem.remove(); );
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
foo.appendChild(btns);
}
<INPUT type="button" value="Add" onclick="add(document.forms[0].value); "/>
<span id="fooBar"><br/></span>
当我添加 setAttribute onlick 时它不起作用。有任何想法吗?