網頁中有一張表格(在我的情況下,http://developer.chrome.com/extensions/api_index),我想要獲得 穩定API中的所有方法名稱。所以我想獲得一個數組,其中的元素是第一列中的東西。如何從表中的第一列獲取文本?
怎麼辦?
$("table:eq(5) tr td:eq(0)")
此代碼不起作用,因爲它不從所有的一切行第一個TD元素獲取文本,但只有一行。該怎麼辦?
網頁中有一張表格(在我的情況下,http://developer.chrome.com/extensions/api_index),我想要獲得 穩定API中的所有方法名稱。所以我想獲得一個數組,其中的元素是第一列中的東西。如何從表中的第一列獲取文本?
怎麼辦?
$("table:eq(5) tr td:eq(0)")
此代碼不起作用,因爲它不從所有的一切行第一個TD元素獲取文本,但只有一行。該怎麼辦?
您可以使用:first-child
選擇器(http://www.w3schools.com/cssref/sel_firstchild.asp),它會爲每一行選擇第一個表格單元格。
你可以嘗試
$(table).find('td:first').text() //can you table id or class
可以在一個循環遍歷所有行獲得第一個單元格值
var array = new Array();
$('table tr').each(function() {
var firstCell = $(this).find('td').first();
array.push($firstCell.text());
});
var array = [];
jQuery('table tr').each(function() {
var $row = $(this);
var $firstCell = $row.find('td:first');
array.push($firstCell.text());
});
嘗試:
var a = new Array();
$("table:eq(5) tr td:first-child").each(
function(){
a.push($(this).text())
});
可以遍歷所有的表格都是這樣的:
$("#tableId > tbody > tr").each(function(){
var name = $(this).find("td:first").text();
console.log("Method name: "+name);
});
您可以全局初始化一個數組並保存在循環中的數組中。希望這可以幫助。