如何从HTML页面获取标签文本值? [英] How to get label text value form a html page?
问题描述
我有一个html页面,其中html呈现为:
< div id ='mViB'>< table id ='myTable'>< tbody>< tr>...</tr>< tr> ...</tr>< tr>...</tr>< tr> ....</tr>< tr>< td>< label id ="* spaM4" for ="* zigField4">真是的< span class ='msde32'></span></label></td></tr></tbody></table></div>
现在,我要执行的操作是获取标签文本"All hell".来自标签.
为此,我同时使用了两个代码: document.getElementById('* spaM4').text
和 document.getElementById('* spaM4').value
他们都没有工作.
我使用了 document.getElementById('* spaM4').innerHTML
,但这也返回了 span类
,但是我只想获取文本./p>
不幸的是,元素ID中的星号是第3方代码,我无法更改.
有人可以建议其他方法来获取标签文字吗?
尝试一下:
document.getElementById('* spaM4').textContent
如果您需要定位<IE9,则需要使用 .innerText
I have a html page , where the html is rendered as :
<div id = 'mViB'>
<table id = 'myTable'>
<tbody>
<tr> ...</tr>
<tr>...</tr>
<tr> ...</tr>
<tr>....</tr>
<tr>
<td>
<label id="*spaM4" for="*zigField4">
All hell.
<span class = 'msde32'></span>
</label>
</td>
</tr>
</tbody>
</table>
</div>
Now what i want to do is get the label text 'All hell.' from the label.
For that purpose i have used both : document.getElementById('*spaM4').text
and document.getElementById('*spaM4').value
but incidentally none of them worked.
I have used document.getElementById('*spaM4').innerHTML
but that returns the span class
as well, but i just want to get the text .
Unfortunately, the asterisks in element IDs are 3rd party code and I cannot change it.
Can any one suggest any other way for getting the label text ?
Try this:
document.getElementById('*spaM4').textContent
If you need to target < IE9 then you need to use .innerText
这篇关于如何从HTML页面获取标签文本值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!