首先发布。我试图使用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中也能这样,但我一直无法找到答案。
答案 0 :(得分: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);
}
感谢那些询问此事的人!尝试不同的事情,我一直在挣扎一个星期。但是,一切都很好。