jQuery计数字符并添加类 [英] jQuery count characters and add class
本文介绍了jQuery计数字符并添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以使用jQuery来计算有多少字符,例如< li>
,如果金额大于XX个字符,请应用一个类到该元素?
Is it possible to use jQuery to count how many characters are in, for example, an <li>
, and if the amount is greater than XX characters, apply a class to that element?
我见过很多jQuery字符计数器(即 http://plugins.jquery.com/plugin-tags/character-counter ),我可以看到 .addClass()
会处理这部分的东西,但是我有一些麻烦把它放在一起。
I've seen plenty of jQuery character counters (ie http://plugins.jquery.com/plugin-tags/character-counter), and I can see how .addClass()
would handle that part of things, but am having some trouble putting it together.
任何指针都会感谢。
非常感谢
感谢每个人的答案 - 找到一个适合我的解决方案,如下所示 >
推荐答案
您可以这样做:
// The number of characters
var XX = 100;
$('li').filter(function() {
return $(this).text().length > XX;
}).addClass('someClass');
这篇关于jQuery计数字符并添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文