javascript - JS - 如何获取outerHTML textContent的字符串?
问题描述
JavaScript 中的问题 - 我使用 tagName 定义了一个元素,我想获取它的完整定义的字符串(如“
let elem = elementsArr[i].outerHTML.textContent;
它返回我未定义。我需要一些帮助:)
只想知道为什么我的代码不起作用
解决方案
只想知道为什么我的代码不起作用
因为.outerHTML
没有方法.textContent
,所以返回undefined
。
如果要获取元素的内容,可以使用.innerHTML
.
或者,如果您只想要文本,您可以.textContent
在元素上使用。
见例子:
d = document.getElementById("d");
console.log(d.outerHTML);
console.log(d.innerHTML);
console.log(d.textContent);
<div id="d"><p>Content</p><p>Further Elaborated</p></div>
推荐阅读
- swift - 如何确保正确处理 VisionImage(来自 Google 在 Firebase 中的 ML Kit)对象?
- javascript - 切换状态组件的未定义 e.target.name
- grep - 如何确定由于特定大小而更高的重叠群数量
- python - Python 代码在 watson-studio 中不起作用?
- python-3.x - 我希望我的播放器在按下按钮时移动
- php - PHP 内联 IF 语句约定
- javascript - 未收到从父级到 iframe 的 JavaScript postMesssage
- python - 用于处理 None 和 with 语句的 Pythonic 方法
- java - 从另一个类引用时获取窗口抛出空指针异常
- c++ - 我可以通过查看二进制文件中的符号来识别未使用的 C++ 标头吗?