运行 ❯
获取您自己的网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>The HTMLCollection Object</h1> <h2>The namedItem() Method</h2> <p id="myElement">The namedItem() method returns the element with the specified id or name.</p> <p>Click "Get" to get the content of the p element with id="myElement":</p> <button onclick="myFunction()">Get</button> <p id="demo"></p> <script> function myFunction() { const collection = document.getElementsByTagName("p"); const element = collection.namedItem("myElement"); document.getElementById("demo").innerHTML = element.innerHTML; } </script> </body> </html>