innerText vs innerHTML vs label vs text vs textContent vs externalText [英] innerText vs innerHTML vs label vs text vs textContent vs outerText

查看:29
本文介绍了innerText vs innerHTML vs label vs text vs textContent vs externalText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 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 included
  • innerHTML will show the value and apply any HTML formatting
  • label appears to be the same as innerText, so I can't see the difference
  • text appears to be the same as innerText but the jQuery shorthand version
  • textContent appears to the same as innerText but keeps formatting (such as )
  • outerText appears to be the same as innerText

我的研究只能带我到我只能测试我能想到的或阅读已发表的内容,任何人都可以确认我的研究是否正确以及标签是否有什么特别之处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 获取所有元素的内容,包括

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