我正在尝试用第二个按钮替换第一个按钮,然后用第三个按钮替换。但是在单击“按钮1”后,Developer Tools控制台会返回:
Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist.
crearBoton2
¿为什么crearBoton在那个特定的环境中不存在?
<html>
<head></head>
<body>
<div id="divi">
<button id="butti">Click Me</button>
</div>
</body>
</html>
和Js:
var getDi = document.getElementById("divi");
var getBu = document.getElementById("butti");
//button 1
function crearBoton (){
var crearBun = document.createElement("button");
crearBun.appendChild(document.createTextNode("Next"));
crearBun.setAttribute('id', 'boton');
getDi.replaceChild(crearBun, getBu);
crearBun.addEventListener('click', crearBoton2);
};
//button 2
function crearBoton2 (){
var getCrearBotonId = document.getElementById("boton");
var crearBud = document.createElement("button");
crearBud.appendChild(document.createTextNode("Next 2"));
getDi.replaceChild(getCrearBotonId, crearBoton);
};
getBu.addEventListener('click', crearBoton);
谢谢!