0

第一次发帖。我正在尝试使用 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 中发生同样的情况,但我一直无法找到答案。

4

1 回答 1

0

我的一个朋友提到我有一个范围问题......下面的代码使网站按照我想要的方式工作。

Javascript:

function removeButtons()
{
var div = document.getElementById("albumown");
var destroyMe = document.getElementById("yesdombutton");   //This makes it so that
var destroyMeToo = document.getElementById("nodombutton"); //the buttons I want to
                                                           //remove are referenced
div.removeChild(destroyMe);                                //correctly
div.removeChild(destroyMeToo);
}

感谢那些问这个问题的人!一个星期以来,我一直在绞尽脑汁尝试不同的事情。但是,现在一切都很好。

于 2013-04-27T22:21:01.617 回答