<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript HTML DOM!</h2> <p>Hello World!</p> <p>Hello Runoob!</p> <p id="demo"></p> <script> var myNodelist = document.querySelectorAll("p"); document.getElementById("demo").innerHTML = "第二个段落的内容为:<span style='color:red;'> " + myNodelist[1].innerHTML + '</span>'; </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript HTML DOM!</h2> <p>Hello World!</p> <p>Hello Runoob!</p> <p id="demo"></p> <script> var myNodelist = document.querySelectorAll("p"); document.getElementById("demo").innerHTML = "文档包含 " + myNodelist.length + " 个段落。"; </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript HTML DOM!</h2> <p>Hello World!</p> <p>Hello Runoob!</p> <p>点击按钮修改所有 p 元素的背景颜色。</p> <button onclick="myFunction()">点我</button> <script> function myFunction() { var myNodelist = document.querySelectorAll("p"); var i; for (i = 0; i < myNodelist.length; i++) { myNodelist[i].style.color = "red"; } } </script> </body> </html>