简体   繁体   English

如何检查X个具有相同类但不同ID的元素是否具有一个特定的CSS属性相同?

[英]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.

相关问题 js-检查相同的类元素是否具有id - js - check if same class elements have id 多个元素使用相同的 css 类,如何仅更改其中一个元素的 css 属性 - Several elements use the same css class, how can I change the css properties of only one of those elements 如何检查具有相同ID的div数量的内容,并向包含特定文本的div添加一个类? - How can I check the content of number of divs with the same ID, and add a class to those that contain a certain piece of text? 如何使用 Javascript 检查具有相同 class 名称的所有元素是否都具有相同的样式属性 - How to check if all elements with same class name all have the same style property using Javascript 如何计算具有相同类的元素的数量? - How can I count the number of elements with same class? 删除具有相同类但不是具有特定 id 的 div - Remove div which have same class but not the one with specific id 当两个元素的ID都以相同的两位数或三位数字结尾时,如何获取一个元素的内容并添加到另一个元素? - How can I take contents of one element and add to another when they both have an id ending in the same two or three digit number? 当我有更多具有相同ID的内容时,如何在ID属性中添加数字表示? - How can i add increament of number in an Id attribut when i have more content with same Id? 如何确定同一页面上的两个元素是否具有相同的ID? - How can I tell if two elements on the same page have the same ID? 如何遍历 class 名称,因此如果两个或多个元素具有相同的数据属性,则它们可以使用 jQuery 具有相同的 ID - How to iterate through class name, so if two or more elements have the same data attributes they can have the same ID using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM