[英]Check charecter length of each p tag jQuery
HTML HTML
<p class="mytext">Lorem ipsum dolor sit amet</p>
<p class="mytext">Lo ipsum dolor sit amet</p>
<p class="mytext">Lorem</p>
Javascript 使用Javascript
$(".mytext").each(function() {
var n = $(this).text().length;
//alert(n)
if (n > 5) {
alert(n + " more than 5");
$(this).css("color", "red");
}
});
I am trying to find character length for each p
for doing some condition. 我试图找到每个
p
字符长度来做某些条件。 can anyone pls help? 任何人都可以帮助吗? JsFiddle
的jsfiddle
You can rather use .filter()
function: 你可以使用
.filter()
函数:
$(".mytext").filter(function(){
return $(this).text().length > 5;
}).css("color" , "red")
Try This: 尝试这个:
$(".mytext").each(function() {
var n = $(this).text().replace(/\s/g, '').length;/* use this to remove all space*/
var n = $.trim($(this).text()).length;/* use this to remove end spaces*/
alert(n)
if (n > 5){
alert(n + " more than 5");
$(this).css("color" , "red");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.