我四处寻找这个问题的答案,但到目前为止都没有成功。我有一个包含带标题的单选按钮的 div
<div>
<label title_id="1" function="update_answer_title" class="pure-radio" for="option-1">
<input type="radio" name="1" value="1" id="option-1">
Talking on the phone or sending a text message.
</label>
<label title_id="2" function="update_answer_title" class="pure-radio" for="option-2">
<input type="radio" name="1" value="2" id="option-2">
Having your keys held in your hand, ready to unlock car and/or provide defense.
</label>
</div>
现在我要做的是在单击标签时获取标签内的文本。不是单选按钮的 html。所以在我的 onclick 监听器中我是到目前为止我尝试过的是:
evt.target.childNodes[1].valueOf().innerText
evt.target.childNodes[1].toString()
JSON.stringify(evt.target.childNodes[1])
以及这些尝试的一系列其他排列。但每次我从这些返回的值中得到的是:
[object Text]
如何将对象文本转换为普通文本?
谢谢!
最佳答案
好吧,那太傻了,我想出来了,而不是做
evt.target.childNodes[1] ...
我需要做的就是获取目标的内部文本,这将自动为我过滤掉 html,所以解决方案是:
evt.target.innerText
编辑:
在对评论的回应中,innerText 在 Firefox 中不存在。所以这样做的方法真的是:evt.target.innerText || evt.target.textContent;
谢谢@Ruslan Osmanov
关于javascript - 获取标签 Javascript 内文本的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36870314/