得到innerContent我有這樣的HTML代碼 我需要從類來獲得=「元素」 innerContent與啓動這樣<th>Element 1</th>
如何從元素
<table class="element">
<thead>
<th>Head of Element 1</th>
<th>Head of Element 2</th>
</thead>
<tbody>
<tr>
<th> Element 1</th>
<th> Element 2</th>
</tr>
</tbody>
</table>
我修整得到這樣的:
var get = window.document.getElementsByClassName('element')[0].getElementsByTagName('th').textContent;
alert(get)
你檢查你的控制檯:
根據利用少一點記憶費利克斯·克林的反應更高級的版本?你會看到這個:'window.document.getElementsByClassName(...)[1]是未定義的'。 –
預期結果是什麼/你得到了什麼結果? – dgiugg
'getElementsByTagName'返回節點的**列表**(getElement ** s ** ByTagName)。只要使用'document.querySelector('。element th')'。 –