请教一下怎么做到判断元素的背景颜色是需要的颜色时就改成白色

新手上路,请多包涵
<div class="news wow fadeInUp animated">
  <p style="margin: 0 25px 0 1px;padding: 0px;background-color: rgb(241, 240, 240);line-height: 21px">11111111111111</p>
  <p style="margin: 0 25px 0 1px;padding: 0px;background-color: rgb(241, 240, 240);line-height: 21px">22222222222222</p>
  <p style="margin: 0 25px 0 1px;padding: 0px;background-color: rgb(241, 240, 240);line-height: 21px">33333333333333</p>
</div>

就是P元素的背景颜色background-color: rgb(241, 240, 240),是这个颜色的时候就自动改成白色也就是rgb(255, 255, 255),如果是其他颜色就不变

阅读 2.7k
3 个回答

如果样式内联的话 偷个懒?

p[style*="background-color: rgb(241, 240, 240)"] {
  background-color: #fff !important;
}
<div class="news wow fadeInUp animated">
    <p style="margin: 0 25px 0 1px;padding: 0px;background-color: rgb(241, 240, 240);line-height: 21px">11111111111111</p>
    <p style="margin: 0 25px 0 1px;padding: 0px;background-color: rgb(241, 240, 240);line-height: 21px">22222222222222</p>
    <p style="margin: 0 25px 0 1px;padding: 0px;background-color: rgb(241, 240, 240);line-height: 21px">33333333333333</p>
  </div>
  <script>
    const p = document.getElementsByTagName("p")
    for(let i = 0;i < p.length;i++){
      const bg = window.getComputedStyle(p[i],null);
      console.log(bg['background-color'])
      if(bg['background-color']==='rgb(241, 240, 240)'){
        p[i].style.backgroundColor="#fff"
      }
    }
  </script>
 $('.news p').each(function(i, v) {
        if (v.style['background-color'] == 'rgb(241, 240, 240)') {
            v.style['background-color'] = 'rgb(255, 255, 255)'
        }
    })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题