【发布时间】:2013-04-28 06:21:01
【问题描述】:
第一次发帖。我正在尝试使用 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 部分中的“是”和“否”按钮,由 JavaScript 创建的按钮按应有的方式出现和消失。
我希望在 Firefox 中出现同样的情况,但我一直找不到答案。
【问题讨论】:
-
你在哪里定义
yesdombutton和nodombutton? -
你在哪里定义
textOne和textTwo? -
@Alnitak -
yesdombutton和nodombutton在makeButtons()中定义。 -
@Qantas94Heavy - 抱歉,我打算删除该代码,因为它仅用于添加按钮上显示的文本,现在编辑!
标签: javascript firefox internet-explorer-10 removechild