JS动态添加style样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<div>
<div>
<div></div>
</div>
</div>
<hr>
<div></div>
</div>
<hr>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
</body>
</html>
<script>
document.addEventListener("DOMContentLoaded", function () {
var css = `
div{
border:3px solid blue;
}
hr{
height:30px;
background-color: #0f0;
}
p{
height:50px;
background-color:pink;
}
`;
const style = document.createElement('style')
style.appendChild(document.createTextNode(css));
document.head.appendChild(style)
})
</script>