InnerText alternative in mozilla [duplicate]

天大地大妈咪最大 提交于 2019-12-18 07:40:29

问题


Does any one know innerText alternative of a span in mozilla? My span is

<span id='cell1'></span>  

and the javascript is

document.getElementById('cell1').innerText = 'Tenelol';

But Mozilla is not supporting this!!


回答1:


innerText is a proprietary IE thing. The W3C defines textContent as the official property.

An easy way is to exploit the || logical operator and its short circuiting nature, as well as JavaScript returning the last evaluated value in a condition (most times the truthy operand).

var body = document.body,
    text = body.textContent || body.innerText;

jsFiddle.

(Note in the fiddle I checked for the innerText first. This was only because most people on here do not use IE. IRL, check for textContent first, and fallback to innerText.)



来源:https://stackoverflow.com/questions/6015388/innertext-alternative-in-mozilla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!