博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
firefox 下 'innerText' works in IE, but not in Firefox
阅读量:2402 次
发布时间:2019-05-10

本文共 2225 字,大约阅读时间需要 7 分钟。

Summary

Gets or sets the text content of a node and its descendents.

Syntax

var text = element.textContent;element.textContent = "this is some sample text";

Description

  • textContent returns null if the element is a , a document type, or a notation. To grab all of the text and CDATA data for the whole document, one could use .textContent.
  • If the node is a CDATA section, a comment, a processing instruction, or a text node, textContent returns the text inside this node (the ).
  • For other node types, textContent returns the concatenation of the textContentattribute value of every child node, excluding comments and processing instruction nodes. This is an empty string if the node has no children.
  • Setting this property on a node removes all of its children and replaces them with a single text node with the given value.

Differences from innerText

Internet Explorer introduced element.innerText. The intention is pretty much the same with a couple of differences:

  • Note that while textContent gets the content of all elements, including and  elements, the mostly equivalent IE-specific property, innerText, does not.
  • innerText is also aware of style and will not return the text of hidden elements, whereas textContent will.
  • As innerText is aware of CSS styling, it will trigger a reflow, whereastextContent will not.

Differences from innerHTML

innerHTML returns the HTML as its name indicates. Quite often, in order to retrieve or write text within an element, people use innerHTMLtextContent should be used instead. Since the text is not processed it's likely to have better performance. Moreover, this avoids an XSS vector attack.

Example

// Given the following HTML fragment://   
This is
some text
// Get the text content:var text = document.getElementById("divA").textContent;// |text| is set to "This is some text".// Set the text content:document.getElementById("divA").textContent = "This is some text";// The HTML for divA is now://
This is some text

Browser compatibility

  •  

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1+ (Yes) 9 (Yes) (Yes)

var myElement = document.getElementById('anyElementId');var myText = (myElement.innerText || myElement.textContent);

转载地址:http://iipob.baihongyu.com/

你可能感兴趣的文章
加载ntfs分区(通过加载支持ntfs内核补丁的方法)
查看>>
修改linux主机名!(网络上存在一些错误)
查看>>
edora core 5 办公环境安装配置,fc5不完全指南(十三)我在飞,加快fc5的启动速度(zt)...
查看>>
Linux启动过程综述(ibm)
查看>>
过并行化 Linux 系统服务来提高引导速度(zt)
查看>>
ORACLE-BASE - Oracle DBA and development articles
查看>>
Congfu Xu's HomePage
查看>>
samba出错!
查看>>
终止进程的工具 kill 、killall、pkill、xkill
查看>>
服务器的提示!
查看>>
biti_rainy
查看>>
系统安装到用raid做成的逻辑驱动器上不能启动的一个原因!
查看>>
tahiti.oracle.com
查看>>
系统安装到用raid做成的逻辑驱动器上不能启动的一个原因!
查看>>
informix 10.0 extent size 的大小限制和数量限制
查看>>
zhouwf0726
查看>>
Oracle字符集问题总结(转贴)
查看>>
SuSE Linux 10.0安装oracle的时候老是提示检查操作系统版本
查看>>
64位SuSE Linux 10.0安装的时候出现黑屏
查看>>
网页特效(三)
查看>>