Apply CSS based on text length of <a href="">
If a href is longer than 40 chars I want apply a class to the p开发者_JAVA技巧revious LI element.
How can I do this in jQuery?
Try this:
$('li a').filter(function () {
return $(this).attr('href').length > 40;
}).closest('li').addClass(...);
Demo here: http://jsfiddle.net/BPQBe/1
Further reading:
filter()
精彩评论