在jquery中获取TH值

时间:2015-07-17 21:01:01

标签: javascript jquery

我正在尝试在数组中获取标题文本,但是随后我获得了价值加上标记,即[<th>value1</th>, <th>value2</th>],我想获得[value1, value2]

$('#header').children().each(function(){this.html});

以下是我的HTML的样子:

<tr bgcolor="#cdb79e" id="header">
    <th>Origin Code</th>
    <th>Description</th>
    <th>Notes</th>
    <th>Domain</th>
    <th>Tier</th>
    <th>Engine</th>
    <th>Network</th>
    <th>Platform</th>
    <th>Expansion Tool</th>
    <th>Date</th>
    <th>Imps</th>
    <th>Clicks</th>
    <th>Engine CTR</th>
    <th>Average Position</th>
    <th>Picks</th>
    <th>LP CTR</th>
    <th>GSL Picks</th>
    <th>GSL LP CTR</th>
    <th>Merchant Picks</th>
    <th>Merchant LP CTR</th>
    <th>CPC</th>
    <th>RPC</th>
    <th>RPP</th>
    <th>Cost</th>
    <th>Total Rev</th>
    <th>Margin</th>
    <th>ROI</th>
</tr>

1 个答案:

答案 0 :(得分:4)

假设您的HTML看起来像这样:

<table>
    <thead>
        <tr id='header'>
            <th>Value1</th>
            <th>Value2</th>
        </tr>
    </thead>
</table>

您可以使用类似的内容来构建<th>元素的数组。文本:

var headerArray = [];
$('#header').children().each(function(){
    headerArray.push($(this).text());
});
console.log(headerArray);