DA_*_*rog 3 javascript jquery html-table
我有一个简单的html表,其中包含thead,tbody和tfoot.我想在thead中选择使用javascript或jquery所有th标签.到目前为止,我找到了一种方法来获得表中的所有内容或thead本身.
我的桌子:
<table id="MyTbl" class="display">
<thead>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Desc
</th>
</tr>
</thead>
<tbody id="MyTableBody">
</tbody>
<tfoot>
<tr height="27px">
<th class="TDHMain">
FILTER ID
</th>
<th class="TDHMain">
FILTER NAME
</th>
<th class="TDHMain">
FILTER DESC
</th>
</tr>
</tfoot>
</table>
Run Code Online (Sandbox Code Playgroud)
我的javascript:
var table = document.getElementById('MyTbl');
var tableHeader = table.getElementsByTagName('thead');
var headers = tableHeader.getElementsByTagName('th'); //This one doesn't work. No such a method for the thead tag
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下代码:
headers = $('#MyTbl').find('thead > th').get();
Run Code Online (Sandbox Code Playgroud)
但结果我真的不明白如何使用它...它不是一个数组,因此我得到的头对象没有foreach函数,我真的找不到一种方法来获取数据.
反正是否只能获得表格中的元素?
谢谢
您可以将所有文本推送到数组中:
var thArray = [];
$('#MyTbl > thead > tr > th').each(function(){
thArray.push($(this).text())
})
Run Code Online (Sandbox Code Playgroud)
然后像这样检索它:
thArray[0]; // This will give you ID
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11597 次 |
| 最近记录: |