Mozilla中的InnerText替代品 [英] InnerText alternative in mozilla

查看:86
本文介绍了Mozilla中的InnerText替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道mozilla中span的innerText替代?
我的跨度是

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

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

和javascript是

and the javascript is

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

但是Mozilla不支持这个!!

But Mozilla is not supporting this!!

推荐答案

innerText 是IE专有的东西。 W3C定义 textContent 作为官方财产。

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

一种简单的方法是利用 || 逻辑运算符及其短路性质,以及JavaScript返回条件中的最后一个评估值(大部分时间是 truthy 操作数)。

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

(请注意我首先检查 innerText 的小提琴。只是因为这里的大多数人不使用IE.IRL,首先检查 textContent ,然后回退到 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.)

这篇关于Mozilla中的InnerText替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆