我正在创建一个重置按钮,当单击时,应通过函数删除自身。
但是,删除仅在按按钮本身以外的任何元素发射时起作用:(我的意思是onclick无法正常工作。
函数创建重置按钮:
function createButton(context, id , value ,func)
{
var button = document.createElement("input");
button.type = "button";
button.id = id;
button.value = value; // text on button
button.onclick = func;
context.appendChild(button); // add the button to the context
}
呼叫创建函数:
createButton(document.body , "resetButton" , "Reset Game" , "resetGame();" );
重置函数,该功能可以删除按钮:
//reminder - id is "resetButton"
function resetGame()
{
var resetBottun = document.getElementById("resetButton");
var parentOfResetButton = resetBottun.parentElement;
parentOfResetButton.removeChild(resetBottun);
};
当我单击按钮时,它应该触发resetGame()函数,并且按钮应消失,但是正如我提到的那样,只有在我从任何其他位置运行resetGame()函数时才会发生,而不是重置按钮的单击。
希望您理解:/
请帮助
button.onclick
的值需要为函数。
您正在分配"resetGame();"
,即 string 。
通过resetGame
代替"resetGame()"
。