【问题标题】:Find all elements with a matching background查找具有匹配背景的所有元素
【发布时间】:2015-12-20 19:36:58
【问题描述】:
我想:
- 在名为“container”的 ID 容器中扫描所有子 div
- 查找具有特定颜色背景颜色的 div(当前定义在变量中,我们可以称之为“currentColor”)
- 替换每一个的背景颜色
对此的任何帮助将不胜感激。
【问题讨论】:
标签:
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');