第一次发帖。我正在尝试使用 HTML 中定义的按钮来调用一个函数,该函数通过 JavaScript 创建按钮,而另一个按钮删除创建的按钮。
HTML:
<div id="thisdiv">
<input type="button" onclick="makeButtons()" value="Add" />
<input type="button" onclick="removeButtons()" value="Remove" />
</div>
Javascript:
function makeButtons()
{
var buttonOne=document.createElement("BUTTON");
var buttonTwo=document.createElement("BUTTON");
buttonOne.setAttribute("id", "yesdombutton"); //adds id to button
buttonTwo.setAttribute("id", "nodombutton"); //adds id to button
document.getElementById("thisdiv").appendChild(buttonOne); //appends buttons
document.getElementById("thisdiv").appendChild(buttonTwo);
}
function removeButtons()
{
var div = document.getElementById("thisdiv");
div.removeChild(yesdombutton); //this works only once in firefox but >
div.removeChild(nodombutton); //works over and over in IE
}
由于 remove() 函数状态中的注释,代码原样只在 Firefox 中运行一次,但在 IE10 中运行良好。通过工作,我的意思是我可以来回单击 HTML 部分中的 Yes 和 No 按钮,JavaScript 创建的按钮按应有的方式出现和消失。
我希望在 Firefox 中发生同样的情况,但我一直无法找到答案。