jquery if元素包含number
我想知道一个元素是否包含一个数字,不管它有多长。我需要区分单词、数字或其他字符jquery if元素包含number,jquery,numbers,Jquery,Numbers,我想知道一个元素是否包含一个数字,不管它有多长。我需要区分单词、数字或其他字符 试试这个。使用isNaN检查它是否是一个数字 $( "li a" ).each(function() { var xc = $(this).text(); var isNum = isNaN(xc); if (isNum) { $(this).attr('class', 'numb'); } else { $
试试这个。使用isNaN
检查它是否是一个数字
$( "li a" ).each(function() {
var xc = $(this).text();
var isNum = isNaN(xc);
if (isNum) {
$(this).attr('class', 'numb');
} else {
$(this).attr('class', 'noNumb');
}
});
你可以用这个函数
正如C-link所说,是数字
$("li a").each(function() {
var toCheck = $(this).text();
if (isNumeric(toCheck)) {
$(this).attr('class', 'is_number');
} else {
$(this).attr('class', 'is_not_number');
}
});
您也可以通过使用正则表达式来实现这一点 调节器exp'/\d/' $(this.text().match(/\d/)
$("li a").each(function() {
var num = $(this).text();
if ($.isNumeric(num)) {
$(this).addClass('numb');
} else {
$(this).addClass('noNumb');
}
});
$("li a").each(function() {
var toCheck = $(this).text();
if (isNumeric(toCheck)) {
$(this).attr('class', 'is_number');
} else {
$(this).attr('class', 'is_not_number');
}
});
$( "li a" ).each(function() {
var matches = $(this).text().match(/\d/);
if (matches){
$(this).attr('class', 'numb');
}else{
$(this).attr('class', 'noNumb');
}
});
$( "li a" ).each(function() {
var xc = $(this).text();
if ( $.isNumeric(xc) ) {
$(this).attr('class', 'numb');
} else {
$(this).attr('class', 'noNumb');
}
});