运行 ❯
获取你
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <style> #myDIV { border: 1px solid black; padding: 16px; } </style> <body> <h1>The Element Object</h1> <h2>The getElementsByTagName() Method</h2> <div id="myDIV"> <h3>A h3 element in div.</h3> <p>P element in div.</p> <span>A span element in div.</span> <h2>A h2 element in div.</h2> <div>A div element in div.</div> <p>Another p element in div.</p> </div> <p>Click the button to add a background color to all elements inside the div element.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { const div = document.getElementById("myDIV"); const nodes = div.getElementsByTagName("*"); for (let i = 0; i < nodes.length; i++) { nodes[i].style.backgroundColor = "red"; } } </script> </body> </html>