<p>欢迎来到牛客网</p> <p>在这里,我们为你提供了IT名企的笔试面试题库</p> <p>在这里,我们以题会友</p> <style> P{ color:red; } </style> <!-- <p style="color:red"style="color:red">欢迎来到牛客网</p> <p style="color:red"style="color:red">在这里,我们为你提供了IT名企的笔试面试题库</p> <p style="color:red">在这里,我们以题会友</p> -->
<style type="text/css"> p { color: rgb(255,0,0); } </style>虽然可以生效且通过,但这里有个问题是题目给出的html代码仅有三个p标签,所以我怀疑这里仅是body标签内部的部分,将style写在这里不符合通常的做法,所决定用JavaScript实现:
let head = document.head; let style = document.createElement("style"); style.type = "text/css"; style.innerHTML = "p {color: rgb(255,0,0);}"; head.appendChild(style);
var p = document.querySelectorAll('p') for(var i = 0;i < p.length;i++){ p[i].style.color = '#f00' }