最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery selector - style values - Stack Overflow

programmeradmin4浏览0评论

I got a series of divs like this:

<div class="message" style="padding-left: 0px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>
<div class="message" style="padding-left: 40px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>

And I would like to make a selector that would get me the divs with padding greater then 20px.

Would it be possible with just using jquery? Or I should modify my html tree and add some attribute that would distinguish those elemenents with high padding value?

I got a series of divs like this:

<div class="message" style="padding-left: 0px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>
<div class="message" style="padding-left: 40px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>

And I would like to make a selector that would get me the divs with padding greater then 20px.

Would it be possible with just using jquery? Or I should modify my html tree and add some attribute that would distinguish those elemenents with high padding value?

Share Improve this question asked Mar 29, 2010 at 10:03 kenderkender 87.4k26 gold badges105 silver badges146 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

You can use a filter with a custom function.

$('div.message').filter(function(){
     return parseInt($(this).css('padding-left')) > 20;
});

p.s. I don't sure what .css('padding') > 20 will return, I'm guess I need to test it....

You can use filter for this.

var elems = $('div.message').filter(function (){
               return parseInt($(this).css("padding-left"),10) > 20;
            });

alert ( elems.length );

or using each you can do something like this

$(function(){
    var elems = new Array();
    $("div.message").each(function(){
        if(parseInt($(this).css("paddingLeft"), 10) > 20 )
        {
            elems.push($(this));
        }           
    });

    alert ( elems.length );
});

You could use $('.message') selector, and then loop through your elements finding the one with .css padding-left set to anything you want.

Second solution, involves usage of custom selectors.

You can modify this code I took from my blog:

$.extend($.expr[':'], {
    redOrBlue: function(o) {
        return ($(o).css("color") == "red") 
               || ($(o).css("color") == "blue");
    }
});

Usage:

$('div:redOrBlue')

You could use filter()

var col = $('div').filter(function ()
{
    return parseInt($(this).css("padding-left")) > 20;
});

or you could create your own selector:

$.expr[':'].myselector = function(obj, index, meta, stack){
   return parseInt($(obj).css("padding-left")) > 20;
};
var col = $("div:myselector");
发布评论

评论列表(0)

  1. 暂无评论