jquery从元素的“this”获取类名

时间:2014-04-02 07:14:53

标签: jquery sharepoint-2010

请告诉我如何从"这个"获得课程名称?在jquery中的每个循环中。 我需要它,因为我想使用类名在第一个

中选择另一个类名

这里是代码。我给了我一个错误

$("#WebPartWPQ3 .ms-itmhover").each(function(){   $(this.attr('class')+" td").css("background","black")   });

4 个答案:

答案 0 :(得分:0)

您可以改为使用 .find()

$("#WebPartWPQ3 .ms-itmhover").each(function(){   
    $(this).find("td").css("background","black")   
});

答案 1 :(得分:0)

它应该是:

$(this).attr('class');

但更清洁的方法是:

 $(this).find("td").css("background","black");  

完整代码:

$("#WebPartWPQ3 .ms-itmhover").each(function(){   
  $(this).find("td").css("background","black");
});

答案 2 :(得分:0)

您可以尝试this.className获取课程名称:

$("#WebPartWPQ3 .ms-itmhover").each(function(){   
    $('.'+this.className+" td").css("background","black"); 
});

但我建议您进入选择器循环的上下文:

$(this).find("td").css("background","black"); 

$("td", this).css("background","black"); 

答案 3 :(得分:0)

.find()

你有错字

$(this.attr('class')+" td")
 //  ^ need to close $(this) and than have to use .find()

$("#WebPartWPQ3 .ms-itmhover").each(function(){   
    $(this).find("td").css("background","black");
});

<小时/> 或

$("#WebPartWPQ3 .ms-itmhover").each(function(){
    var cls = $(this).attr('class').split(' ').join('.');
    $('.'+ cls +" td").css("background","black")   
});