运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
转到 Spaces
<!DOCTYPE html> <html> <body> <h1>The HTMLCollection Object</h1> <h2>The namedItem() Method</h2> <p id="myElement">This example uses a shorthand method for the namedItem() method.</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["myElement"]; document.getElementById("demo").innerHTML = element.innerHTML; } </script> </body> </html>