gpt4 book ai didi

angular - 从 ElementRef 获取文本?

转载 作者:太空狗 更新时间:2023-10-29 17:33:28 27 4
gpt4 key购买 nike

感觉这应该是一个很基础的问题...

我正在使用一个返回不可见/不呈现的 DOM 节点/元素的插件。我能够轻松地将它转换为 ElementRef,并正确填充 nativeElement。

如何获取元素的文本?

我知道使用 AngularJS,我会做 angular.elem(node).text()

最佳答案

要获取元素的文本内容,请使用 textContentinnerText属性:

elementRef.nativeElement.textContent
elementRef.nativeElement.innerText

this stackblitz 中显示了一个示例.


以下是它们之间的一些差异,如 the MDN documentation 中所示:

  • While textContent gets the content of all elements, including <script> and <style> elements, innerText does not.
  • innerText is aware of style and will not return the text of hidden elements, whereas textContent will.
  • As innerText is aware of CSS styling, it will trigger a reflow, whereas textContent will not.
  • Unlike textContent, altering innerText in Internet Explorer (up to version 11 inclusive) not only removes child nodes from the element, but also permanently destroys all descendant text nodes (so it is impossible to insert the nodes again into any other element or into the same element anymore).

关于angular - 从 ElementRef 获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49931404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com