admin管理员组

文章数量:1297051

How can I select all elements that have a specific CSS property applied, using jQuery? For example:

.Title
{
    color:red;
    rounded:true;
}

.Caption
{
    color:black;
    rounded:true;
}

How to select by property named "rounded"?

CSS class name is very flexible.

$(".Title").corner();
$(".Caption").corner();

How to replace this two operation to one operation. Maybe something like this:

$(".*->rounded").corner();

Is there any better way to do this?

How can I select all elements that have a specific CSS property applied, using jQuery? For example:

.Title
{
    color:red;
    rounded:true;
}

.Caption
{
    color:black;
    rounded:true;
}

How to select by property named "rounded"?

CSS class name is very flexible.

$(".Title").corner();
$(".Caption").corner();

How to replace this two operation to one operation. Maybe something like this:

$(".*->rounded").corner();

Is there any better way to do this?

Share Improve this question edited May 19, 2014 at 17:06 Keavon 7,4639 gold badges55 silver badges83 bronze badges asked Aug 3, 2009 at 6:02 ebattulgaebattulga 11k20 gold badges79 silver badges119 bronze badges 1
  • 12 Hey there, folks: Please read the question before answering! This poor guy got four out of five completely wrong answers. – Boldewyn Commented Aug 3, 2009 at 6:22
Add a comment  | 

6 Answers 6

Reset to default 75

This is a two year old thread, but it was still useful to me so it could be useful to others, perhaps. Here's what I ended up doing:

var x = $('.myselector').filter(function () { 
    return this.style.some_prop == 'whatever' 
});

not as succinct as I would like, but I have never needed something like this except now, and it's not very efficient for general use anyway, as I see it.

Thank you, Bijou. I used your solution, but used the jQuery .css instead of pure javascript, like this:

var x = $('*').filter(function() {
    return $(this).css('font-family').toLowerCase().indexOf('futura') > -1
})

This example would select all elements where the font-family attribute value contains "Futura".

You cannot (using a CSS selector) select elements based on the CSS properties that have been applied to them.

If you want to do this manually, you could select every element in the document, loop over them, and check the computed value of the property you are interested in (this would probably only work with real CSS properties though, not made up ones such as rounded). It would also would be slow.

Update in response to edits — group selectors:

$(".Title, .Caption").corner();

Similar as Bijou's. Just a little bit enhancement:

$('[class]').filter(function() {
    return $(this).css('your css property') == 'the expected value';
  }
).corner();

I think using $('[class]') is better:

  • no need to hard code the selector(s)
  • won't check all HTML elements one by one.

Here is an example.

Here is a clean, easy to understand solution:


// find elements with jQuery with a specific CSS, then execute an action
$('.dom-class').each(function(index, el) {
    if ($(this).css('property') == 'value') {
        $(this).doThingsHere();
    }
});

This solution is different because it does not use corner, filter or return. It is intentionally made for a wider audience of users.

Things to replace:

  1. Replace ".dom-class" with your selector.
  2. Replace CSS property and value with what you are looking for.
  3. Replace "doThingsHere()" with what you want to execute on that found element.

Good luck!

Custom CSS properties aren't inherited, so must be applied directly to each element (even if you use js to dynamically add properties, you should do it by adding a class), so...

CSS

.Title
{
    color:red;
}

.Caption
{
    color:black;
}

HTML

You don't need to define a rounded:true property at all. Just use the presence of the 'Rounded' class:

<div class='Title Rounded'><h1>Title</h1></div>
<div class='Caption Rounded'>Caption</div>

JS

jQuery( '.Rounded' ).corner();

本文标签: javascriptSelect all elements that have a specific CSSusing jQueryStack Overflow