开发者

InnerText alternative in mozilla [duplicate]

开发者 https://www.devze.com 2023-03-06 09:17 出处:网络
This question already has answers here: 'innerText' works in IE, but not in Firefox 开发者_运维问答
This question already has answers here: 'innerText' works in IE, but not in Firefox 开发者_运维问答 (15 answers) Closed 7 years ago.

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!!


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消