尝试使用jquery检索类名时出错

尝试使用jquery检索类名时出错,jquery,Jquery,我有以下代码 var allRows = $('.myclass'); ... allRows.each(function() { //now search through all the rows var className = this.attr("class"); ... }); var allRows=$('.myclass'); ... allRows.each(function(){//现在搜索所有行 var cla

我有以下代码

var allRows = $('.myclass'); ... allRows.each(function() { //now search through all the rows var className = this.attr("class"); ... }); var allRows=$('.myclass'); ... allRows.each(function(){//现在搜索所有行 var className=this.attr(“类”); ... }); 我收到一条错误消息

Uncaught TypeError: Object #<HTMLDivElement> has no method 'attr'
uncaughttypeerror:对象#没有方法“attr”
我的代码是什么?我在allRows上做了一个console.log,它是一个jquery对象。

您应该试试这个

 var className = $(this).attr("class");// attr is jquery function
 console.log(className);
完整代码

var allRows = $('.myclass');
...
allRows.each(function() {   //now search through all the rows
    var className = $(this).attr("class");// change this to $(this) here
    console.log(className);            
});
更改为:

var className = $(this).attr("class");

您应该将
更改为
$(此)


您还可以使用
className

var allRows = $('.myclass');
allRows.each(function () { //now search through all the rows
    var className = this.className;
});
您没有正确使用“this”。下面给出了正确的方法:-

    var allRows = $('.myclass');
       ...
    $(allRows).each(function() {   //now search through all the rows
       var className = $(this).attr("class");
       ...    
    });
当然,谢谢!attr()是jquery方法,因此您当然必须首先将其传递给jquery。
    var allRows = $('.myclass');
       ...
    $(allRows).each(function() {   //now search through all the rows
       var className = $(this).attr("class");
       ...    
    });