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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:09:33  来源:igfitidea点击:

InnerText alternative in mozilla

javascriptinnertext

提问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

innerTextis a proprietary IE thing. The W3C definestextContentas 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;

jsFiddle.

js小提琴

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

(注意在小提琴中我检查了第innerText一个。这只是因为这里的大多数人不使用 IE。IRL,textContent首先检查,然后回退到innerText。)