2014-02-17 59 views
1

我通過TR使用此代碼迴路中的CSS類特定TD文本我怎麼能找到其中TR存儲爲這個

$('#prod_tbl > tbody > tr').each(function() { 
          var x = $(this); 

       }); 

現在我想的是我有不同的類名稱TD什麼像。價格,.NAME

我想訪問不同變量中的每個TD的值,當i循環通過TR

喜歡

 $('#prod_tbl > tbody > tr').each(function() { 
            var x = $(this); 
var price=$("td.price").text(); 
var name= $("td.name").text(); 


         }); 

對於每個TR

回答

1

您必須在此上下文中使用.find()才能完成您的任務。那是因爲td是每個Tr的後代。 $(this)將指向Tr,所以我們必須使用.find(selector)找到它匹配的後代,

嘗試,

$('#prod_tbl > tbody > tr').each(function() { 
    var x = $(this); 
    var price= x.find("td.price").text(); 
    var name = x.find("td.name").text(); 
}); 

請仔細閱讀here更多地瞭解.find()

1

使用.find()找到td哪些是當前的後代tr元件

$('#prod_tbl > tbody > tr').each(function() { 
    var x = $(this); 
    var price = $(this).find("td.price").text(); 
    var name = $(this).find("td.name").text(); 


}); 
1

您正在選擇的所有類別爲pricename。在上下文中通過trjQuery(selector [, context ])或使用find()獲得當前行的後代中的td。

使用context in selector

var price=$("td.price", this).text(); 
var name= $("td.name", this).text(); 

使用find()

var price = $(this).find("td.price").text(); 
var name = $(this).find("td.name").text();