javascript - 如何在不更改子元素的情况下更改元素 innerText
问题描述
我有一个 html 元素,例如:
<div id="el1">Change only me<div>but not me</div></div>
但我只想更改第一个文本并保留子 div 原样
document.getElementById("el1").innerText = "changed!"
<div id="el1">Change only me<div>but not me</div></div>
解决方案
尝试这个:
document.getElementById("el1").childNodes[0].textContent = "changed";
<div id="el1">Change only me<div>but not me</div></div>
推荐阅读
- amazon-web-services - 我如何(可以)配置 AWS SNS HTTP 请求超时?
- postgresql - 在 spring boot 中使用 flywaydb 和 google cloud sql 的问题
- oracle-apex-5 - 在 oracle apex 中运行的情况
- c - scanf 在我按两次 Enter 之前不接受输入
- html - 网页抓取网页的特定部分
- c# - 使用多种方法创建一个简单的计算器
- r - 跨面板叠加文本
- excel - 无法使用目标复制将一个单元格复制到另一个单元格
- cpanel - 使用 cpanel 在服务器上设置 laravel 项目
- scala - 构建 Fat JAR 时类型安全应用程序和参考配置文件未合并