运行 ❯
获取您自己的网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>The Element Object</h1> <h2>The replaceChild() Method</h2> <ul id="myList"> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul> <p>Click the button to replace the first item in the the list.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { const element = document.createElement("li"); const textNode = document.createTextNode("Water"); element.appendChild(textNode); const list = document.getElementById("myList"); list.replaceChild(element, list.children[0]); } </script> </body> </html>