javascript Mozilla 中的 InnerText 替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6015388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
InnerText alternative in mozilla
提问by Varada
Does any one know innerText alternative of a span in mozilla? My span is
有没有人知道 mozilla 中 span 的 innerText 替代方案?我的跨度是
<span id='cell1'></span>
and the javascript is
和 javascript 是
document.getElementById('cell1').innerText = 'Tenelol';
But Mozilla is not supporting this!!
但是 Mozilla 不支持这个!!
回答by alex
innerText
is a proprietary IE thing. The W3C definestextContent
as the official property.
innerText
是 IE 专有的东西。在W3C定义textContent
为官员财产。
An easy way is to exploit the ||
logical operator and its short circuitingnature, as well as JavaScript returning the last evaluated value in a condition (most times the truthyoperand).
一种简单的方法是利用||
逻辑运算符及其短路特性,以及 JavaScript 返回条件中的最后一个评估值(大多数情况下是真操作数)。
var body = document.body,
text = body.textContent || body.innerText;
(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
.)
(注意在小提琴中我检查了第innerText
一个。这只是因为这里的大多数人不使用 IE。IRL,textContent
首先检查,然后回退到innerText
。)