Html innerText 和outerText 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18481382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:43:45  来源:igfitidea点击:

What is the difference between innerText and outerText?

htmldominnertext

提问by Abhishek Singh

After searching through web i understood the difference between innerHTML and outerHTML.

通过网络搜索后,我了解了innerHTML 和outerHTML 之间的区别。

However i am having hard time understanding the difference between innerText and outerText. Both appear almost same to me.

但是我很难理解innerText 和outerText 之间的区别。两者在我看来几乎相同。

Can anyone help me understand this with a nice illustration ?

谁能用漂亮的插图帮助我理解这一点?

Thank You !

谢谢你 !

回答by CodingIntrigue

innerTextchanges only text within HTML tags, e.g.

innerText仅更改 HTML 标签内的文本,例如

<div>
  <p>Change Me</p>
</div>

p.innerText = "Changed!"

Becomes

成为

<div>
  <p>Changed!</p>
</div>

Whereas outerText:

鉴于outerText

<div>
  <p>Change Me</p>
</div>

p.outerText = "Changed!"

Becomes

成为

<div>
   Changed!
</div>

回答by Sadiq

Basically,
innerText: what's between the tags of the element.
outerText: content of the element, includingthe tags.

基本上,
innerText:元素标签之间的内容。
externalText:元素的内容,包括标签。