JavaScript のtextContent
との違いは何ですか?innerText
textContent
以下のように使えますか?
var logo$ = document.getElementsByClassName('logo')[0];
logo$.textContent = "Example";
ベストアンサー1
innerText
との主な違いはtextContent
、Kelly Norton のブログ投稿: innerText と textContent以下に概要を示します。
innerText
非標準でしたが、textContent
以前に標準化されました。innerText
はノードに含まれる可視テキストを返し、 は全文textContent
を返します。たとえば、次の HTML では、は「Hello」を返し、 は「Hello World」を返します。より完全な相違点の一覧については、次の表を参照してください。<span>Hello <span style="display: none;">World</span></span>
innerText
textContent
http://perfectionkills.com/the-poor-misunderstood-innerText/(詳しくは「innerText」はIEでは動作しますが、Firefoxでは動作しません)。- その結果、
innerText
パフォーマンスが大幅に低下します。結果を返すにはレイアウト情報が必要になります。 innerText
HTMLElement
はオブジェクトに対してのみ定義されますが、textContent
すべてのオブジェクトに対して定義されますNode
。
この回答の下にある有益なコメントも必ずご覧ください。
textContent
IE8 では利用できず、ベアメタル ポリフィルは指定されたすべてのノードnodeValue
でを使用する再帰関数のように見えました。childNodes
function textContent(rootNode) {
if ('textContent' in document.createTextNode(''))
return rootNode.textContent;
var childNodes = rootNode.childNodes,
len = childNodes.length,
result = '';
for (var i = 0; i < len; i++) {
if (childNodes[i].nodeType === 3)
result += childNodes[i].nodeValue;
else if (childNodes[i].nodeType === 1)
result += textContent(childNodes[i]);
}
return result;
}