I have a table
<table>
<tr class="PO1"></tr>
<tr class="PO2"></tr>
<tr class="PO3"></tr>
</table>
How can I loop through all tr with class "PO1"
and get the value of each 'td'
value?
$("t开发者_JS百科able#id tr .PO1").each(function(i)
{
// how to get the td values??
});
var values = $('table tr.PO1 td').map(function(_, td) {
return $(td).text();
}).get();
This would just create an array with the text contents from each td
. Probably a better idea to use a map/object instead:
var values = $('table tr.PO1 td').map(function(index, td) {
var ret = { };
ret[ index ] = $(td).text();
return ret;
}).get();
The space before the .P01 is what's breaking your current code.
$("tr.PO1 td").each(function(i){
$(this).text()
});
notice : I removed a space before .PO1 because your tr has class P01
$("table#id tr.PO1").each(function(i)
{
$(this).find("td").innerHTMl() //for example
});
$("table#id tr.PO1").each(function(i)
{
i.children('td').each(function(tdEL) {
// tdEl.val();
});
});
Notice the space I removed between tr and .PO1. In you case it will try to find each tr with an child having the class .PO1.
精彩评论