請告訴我如何從jquery的每個循環中的「this」獲得類名。 我需要它,因爲我想使用類名來選擇第一個jquery從一個元素的「this」中獲得類名
以下是代碼。 I'ts給我一個錯誤
$("#WebPartWPQ3 .ms-itmhover").each(function(){ $(this.attr('class')+" td").css("background","black") });
請告訴我如何從jquery的每個循環中的「this」獲得類名。 我需要它,因爲我想使用類名來選擇第一個jquery從一個元素的「this」中獲得類名
以下是代碼。 I'ts給我一個錯誤
$("#WebPartWPQ3 .ms-itmhover").each(function(){ $(this.attr('class')+" td").css("background","black") });
你有錯字
$(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")
});
您可以使用.find()代替:
$("#WebPartWPQ3 .ms-itmhover").each(function(){
$(this).find("td").css("background","black")
});
它應該是:
$(this).attr('class');
而是一個更清潔的方式來做到這一點是:
$(this).find("td").css("background","black");
完整代碼:
$("#WebPartWPQ3 .ms-itmhover").each(function(){
$(this).find("td").css("background","black");
});
你可以嘗試this.className
獲取類名:
$("#WebPartWPQ3 .ms-itmhover").each(function(){
$('.'+this.className+" td").css("background","black");
});
而是我向你推薦你選擇循環的環境中去:
$(this).find("td").css("background","black");
或
$("td", this).css("background","black");
_I要使用的類名到first_內選擇其他類的名稱..儘管你有很多答案,但還是有更多的信息你的帖子。 – Jai
以及事情是我想實際遍歷所有錶行,並從特定行的表單元格中提取單個值。這條線只是爲了測試,如果我得到正確的列。無論如何,我認爲最好先爲每個錶行分配一個唯一的id,然後訪問它的嵌套元素。 –
Sharepoint是一個網絡開發者的噩夢,呵呵 –