如果div包含数字,则为div添加一个类

时间:2014-09-22 18:41:47

标签: jquery

好的,应该很容易,无法弄清楚它为什么不起作用。

类调用toggle_comments,使用正则表达式来挖掘数字,如果它为零,则为它添加一个类,简单吗?

$(document).ready(function () {
 $( ".toggle_comments" ).each(function() {
var xc = $(this).text().length;
    var matches = $(this).text().match(/^0$/);
    if (matches){
       $(this).attr('class', 'numb');    
    }
});

});

没有用。

1 个答案:

答案 0 :(得分:0)

只需将正则表达式更改为此/0+/g即可。这应该可以解决你的问题。