innerText vs innerHTML vs label vs text vs textContent vs externalText [英] innerText vs innerHTML vs label vs text vs textContent vs outerText
问题描述
我有一个由 Javascript 填充的下拉列表.
I have a dropdown list which is populated by Javascript.
在决定加载时显示的默认值时,我意识到以下属性显示的值完全相同:
Whilst deciding what should be the default value to show on load, I realised that the following properties showed exactly the same values:
innerText
innerHTML
标签
text
textContent
outerText
我自己的研究显示了基准测试或其中一些之间的比较,但不是全部.
My own research shows bench marking tests or comparisons between a few of them, but not all.
我可以使用我自己的常识并选择 1 或另一个,因为它们提供相同的结果,但是,我担心如果数据发生变化,这将不是一个好主意.
I can use my own common sense and choose 1 or the other as they provide the same result, but, I'm concerned this is not going to be a good idea if the data were to change.
我的发现是:
innerText
将按原样显示值并忽略可能包含的任何 HTML 格式innerHTML
将显示值并应用任何 HTML 格式label
似乎和innerText
一样,所以我看不出区别text
似乎与innerText
相同,但 jQuery 速记版本textContent
看起来与innerText
相同,但保持格式(例如outerText
似乎与innerText
相同
innerText
will show the value as is and ignores any HTML formatting which may be includedinnerHTML
will show the value and apply any HTML formattinglabel
appears to be the same asinnerText
, so I can't see the differencetext
appears to be the same asinnerText
but the jQuery shorthand versiontextContent
appears to the same asinnerText
but keeps formatting (such asouterText
appears to be the same asinnerText
我的研究只能带我到我只能测试我能想到的或阅读已发表的内容,任何人都可以确认我的研究是否正确以及标签是否有什么特别之处
和 outerText
?
My research can only take me so far as I can only test what I can think of or read what is published, can any one confirm though if my research is correct and if there is anything special about label
and outerText
?
推荐答案
来自 MDN:
Internet Explorer 引入了 element.innerText.[作为 textContent] 的意图几乎相同,但有一些不同:
Internet Explorer introduced element.innerText. The intention is pretty much the same [as textContent] with a couple of differences:
请注意,虽然 textContent 获取所有元素的内容,包括
和
元素,但大多数等效于 IE 特定属性,innerText,没有.
Note that while textContent gets the content of all elements, including
<script>
and<style>
elements, the mostly equivalent IE-specific property, innerText, does not.
innerText 也知道样式,不会返回隐藏元素的文本,而 textContent 会.
innerText is also aware of style and will not return the text of hidden elements, whereas textContent will.
由于innerText 知道CSS 样式,它会触发重排,而textContent 不会.
As innerText is aware of CSS styling, it will trigger a reflow, whereas textContent will not.
所以 innerText
不会包含被 CSS 隐藏的文本,但 textContent
会.
So innerText
will not include text that is hidden by CSS, but textContent
will.
innerHTML 返回如其名称所示的 HTML.很多时候,为了在元素中检索或写入文本,人们使用innerHTML.应该使用 textContent 代替.因为文本不会被解析为 HTML,所以它可能具有更好的性能.此外,这避免了 XSS 攻击向量.
innerHTML returns the HTML as its name indicates. Quite often, in order to retrieve or write text within an element, people use innerHTML. textContent should be used instead. Because the text is not parsed as HTML, it's likely to have better performance. Moreover, this avoids an XSS attack vector.
如果你错过了,让我更清楚地重复一遍:不要不要使用 .innerHTML
除非你特别打算在一个元素中插入 HTML 并且有采取必要的预防措施以确保您插入的 HTML 不能包含恶意内容.如果您只想插入文本,请使用 .textContent
或者如果您需要支持 IE8 及更早版本,请使用功能检测在 .textContent
和 .innerText 之间切换
.
In case you missed that, let me repeat it more clearly: Do not use .innerHTML
unless you specifically intend to insert HTML within an element and have taken the necessary precautions to ensure that the HTML you are inserting cannot contain malicious content. If you only want to insert text, use .textContent
or if you need to support IE8 and earlier, use feature detection to switch off between .textContent
and .innerText
.
有这么多不同的属性的一个主要原因是不同的浏览器最初对这些属性有不同的名称,并且仍然没有对所有这些属性提供完整的跨浏览器支持.如果你使用 jQuery,你应该坚持使用 .text()
因为它旨在消除跨浏览器的差异.*
A main reason that there are so many different properties is that different browsers originally had different names for these properties, and there still isn't complete cross-browser support for all of them. If you are using jQuery, you should stick to .text()
since that is designed to smooth out cross-browser differences.*
对于其他一些:outerHTML
与 innerHTML
基本相同,除了它包括它所属元素的开始和结束标记.我似乎根本找不到关于 outerText
的太多描述.我认为这可能是一个不起眼的遗留财产,应该避免.
For some of the others: outerHTML
is basically the same as innerHTML
, except that it includes the start and end tags of the element it belongs to. I can't seem to find much description of outerText
at all. I think that is probably an obscure legacy property and should be avoided.
这篇关于innerText vs innerHTML vs label vs text vs textContent vs externalText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!