开发者

Apply CSS based on text length of <a href="">

开发者 https://www.devze.com 2023-01-03 16:23 出处:网络
If a href is longer than 40 chars I want apply a class to the p开发者_JAVA技巧revious LI element.

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()
0

精彩评论

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