querySelectorAll选择基于css

这是我遇到的问题的简单表示。当您单击按钮时,所有黑色 div 都应变为黑色。但是用 css 样式的大的没有。有任何想法吗?多谢。


var v;


function link() {

  v = document.querySelectorAll('[style*="background:black;"]');

  v.forEach(function(value) {

    value.style.backgroundColor = "red";

  });

}

.box1 {

  background-color: black;

  height: 200px;

  width: 200px;

  border: 2px solid green;

  font-size: 25px;

  color: white;

}

<html>


<head>

  <link rel="stylesheet" href="filter.css" />

</head>


<body>


  <div style="background:black; height:100px; width:100px;" id="div1"></div>

  <div style="background:blue; height:100px; width:100px;"></div>

  <div style="background:black; height:100px; width:100px;"></div>

  <div class="box1"> css styled box</div>

  <button onclick="link()" style="height:20px; width:50px;">Click </button>


</body>


</html>


慕运维8079593
浏览 146回答 1
1回答

沧海一幻觉

试试这个var v;function link(){&nbsp; v=document.querySelectorAll('[style*="background:black;"]');&nbsp; v.forEach(function(value){&nbsp; &nbsp; value.style.backgroundColor = "red";&nbsp; });}.box1{&nbsp; height:200px;&nbsp; width:200px;&nbsp; border:2px solid green;&nbsp; &nbsp; font-size:25px;&nbsp; &nbsp; color:white;}<html><head><link rel="stylesheet" href="filter.css" /></head><body><div style="background:black; height:100px; width:100px;" id="div1"></div><div style="background:blue; height:100px; width:100px;"></div><div style="background:black; height:100px; width:100px;"></div><div class="box1" style="background:black;"> css styled box</div><button onclick="link()" style="height:20px; width:50px;">Click </button></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript