获取标签Javascript中文本的值
我四处寻找答案,但到目前为止都没有成功。我有一个包含标题单选按钮的div获取标签Javascript中文本的值,javascript,html,Javascript,Html,我四处寻找答案,但到目前为止都没有成功。我有一个包含标题单选按钮的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 t
<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>
以及这些尝试的一系列其他排列。但每次我从这些返回的值中得到的都是:
[object Text]
如何将对象文本转换为普通文本
谢谢大家! 那太傻了,我想出来了,而不是去做
evt.target.childNodes[1] ...
我所需要做的就是获取目标的内部文本,这将自动过滤掉html,因此解决方案是:
evt.target.innerText
编辑:
作为对评论的回应,Firefox中不存在innerText。所以实现这一点的方法是:evt.target.innerText | | evt.target.textContent
感谢@Ruslan Osmanov使用
evt.target.textContent | | evt.target.innerText
获得右侧的兼容性innerText
不是标准版本,Firefox也没有。Firefox从45版开始实施了innerText
!再发布几次之后,退出textContent
可能是安全的。
evt.target.innerText