javascript - What's the use of textContent/innerText when innerHTML does the job better? -
this might newbie question of explained me innertext gets element's text , can't modified using html tags, while innerhtml same job , html can used. so, what's point in having of them?
advantages of textcontent
on innerhtml
:
it works on nodes, not elements.
var node = document.createtextnode('hello'); node.innerhtml; // undefined node.textcontent; // 'hello'
it gets text contents of element, without having strip html tags manually.
var el = document.createelement('div'); el.innerhtml = 'a<p>b<span>c</span>d</p>d'; el.textcontent; // "abcdd" (html tags stripped successfully)
it sets contents of element bunch of plain text, without having html-escape it.
var el = document.createelement('div'); el.textcontent = 'a<p>b<span>c</span>d</p>d'; el.children.length; // 0 (plain text html-escaped successfully)
sure, when use them on elements, innerhtml
can more powerful. when care text content not html content, textcontent
simpler , have better performance.
so have both of them, , choose appropriate each case.
Comments
Post a Comment