开发者

Change Numerical Class

I currently have a wordpress blog that assigns a number to each one of its posts.. It also takes this number and applies it to the container that the post sits in like so "post-813"

Is there a way with jQuery to where I can say.. If the class is post-810 or greater, add class "new" ??

Thanks

<div class="post-813 category-uncategorized">
...
</div>

Edit:

I was actually able to get it t开发者_如何学JAVAo print an ID like so:

<div id="post-810">

I was able to get the first ID, but now I need to do it for each. How is this possible?

 var number = $('div.post').attr("id");

 var trimmed = number.replace("post-", "");


You can make your own selector to help with this. Since you've got the divs assigned with Ids, it will make writing the selector a bit easier. First, add this to your code.

$.extend($.expr[':'], {
                post810: function(e) {
                    var id = $(e).attr("id");
                    var post = id.replace("post-", "");
                    return (post >= 810);
                }
            });

Then you can do this to add classes:

$(':post810').addClass('new')


I don't think there is one ready-made, but you can easily write a custom JQuery Filter for it. See filter() documentation and examples


try this:

var patt = /post-(\d+)/g;
$("div[class*='post']").filter(function(){
    var result = patt.exec($(this).attr("class"));
    return result !== null && parseInt(result[1], 10) >= 810;
}).addClass("new");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜