如何更改元素innerText而不更改其子元素

时间:2020-05-26 16:05:36

标签: javascript html parent-child 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>

1 个答案:

答案 0 :(得分:4)

尝试一下:

import {USER_LOGIN_SUCCESS, SET_SIDEBAR_VISIBILITY} from 'react-admin'
document.getElementById("el1").childNodes[0].textContent = "changed";