【问题标题】:Find all elements with a matching background查找具有匹配背景的所有元素
【发布时间】:2015-12-20 19:36:58
【问题描述】:

我想:

  1. 在名为“container”的 ID 容器中扫描所有子 div
  2. 查找具有特定颜色背景颜色的 div(当前定义在变量中,我们可以称之为“currentColor”)
  3. 替换每一个的背景颜色

对此的任何帮助将不胜感激。

【问题讨论】:

  • 你自己尝试过什么吗?
  • 是的。我们还需要一些演示。你的问题不清楚
  • 您有什么工作或成果吗?你做过什么不成功的尝试?

标签: jquery background-color


【解决方案1】:

扫描一个名为“container”的 ID 容器中的所有子 div

$('#container div').each(function(){
    console.log(this)
});

查找具有某种颜色背景颜色的div(当前定义在一个变量中,我们可以称之为“currentColor”)

$('#container div').filter(function(){
    return $(this).css('background-color') == currentColor ; 
});

替换每一个的背景颜色

$('#container div').filter(function(){
    return $(this).css('background-color') == currentColor ;
}).css('background-color', 'newcolor'); 

【讨论】:

  • 效果很好。代码也很优雅。谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
  • 2013-12-06
  • 2017-03-15
  • 2022-01-11
  • 2016-07-02
相关资源
最近更新 更多