[英]How can i check if X number of elements with same class but different id have one specific css property identical?
How can i check if X number of elements with same class
but different id
have one specific css property identical? 如何检查X个具有相同
class
但不同id
的元素是否具有一个特定的CSS属性相同? Imagine that there are 20 elements or somewhere like this with same class. 想象一下,同一个类中有20个元素或类似的地方。 Like here
像这儿
<div class="back" id="i1"></div>
<div class="back" id="i2"></div>
<div class="back" id="i3"></div>
<style>
back{width:100px;height:100px;float:left;}
#i1{background-image:url(img1.jpg);}
#i2{background-image:url(img2.jpg);}
</style>
And if they are identical i add a class but i suppose that would be easy. 如果它们相同,我会添加一个类,但是我想那很容易。 An example of what i have is here
我所拥有的一个例子在这里
To test css properties you can use .css()
, for example: 要测试css属性,可以使用
.css()
,例如:
if ($('#i1').css('background-image') == $('#i2').css('background-image')) {
// do something
}
Let's say you want to check whether all the ".back" divs have the same background-image
(I hope it's what you're trying to achieve). 假设您要检查所有“ .back” div是否具有相同的
background-image
(我希望这是您要实现的目标)。 You could go with something like this: 你可以这样:
var same = 1;
var property = $(".back").first().css('background-image');
$(".back").each(function() {
if ($(this).css('background-image') != property)
same = 0;
});
if (same == 1) { // all properties where the same
// do something
}
I can't really see where your going with this, but you can do something on these lines. 我真的看不到您要怎么做,但是您可以在这些行上做些事情。
var arr = [];
$.each($('.back'), function() {
arr.push($(this).css('background-image'));
});
Or maybe, 或者可能,
$.each($('.back'), function() {
if ($(this).css('background-image') === 'lookingForThis.jpg') {
alert('Found it!');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.