3 回答
TA贡献1807条经验 获得超9个赞
lastChild您可以使用或定位最后一个文本节点,childNodes[lastIndex] 并使用以下命令更改其值node.nodeValue="Something"
document.getElementById("notificationUnread").lastChild.nodeValue="Test"
或者
let nodes=document.getElementById('notificationUnread').childNodes
nodes[nodes.length-1].nodeValue= "TES"
//let nodes=document.getElementById('notificationUnread').childNodes
//nodes[nodes.length-1].nodeValue= "TES"
//Without span
document.getElementById("notificationUnread").lastChild.nodeValue="Test Without span"
//With span
document.querySelector("#notificationUnread2 > .text-notification").innerText="Test with span"
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<!--without span -->
<a id="notificationUnread" class="dropdown-item">
<i class="fas fa-bell mr-2"></i> 0 unread notifications
</a>
<br>
<!--with span -->
<a id="notificationUnread2" class="dropdown-item">
<i class="fas fa-bell mr-2"></i>
<span class="text-notification">0 unread notifications</span>
</a>
TA贡献1820条经验 获得超10个赞
将要更改的文本包装在<span>
<a id="notificationUnread" class="dropdown-item">
<i class="fas fa-envelope mr-2"></i> <span class="link-text">0 unread notifications<span>
</a>
document.querySelector('#notificationUnread .link-text').innerText = "TEST"
TA贡献1829条经验 获得超6个赞
如果您想更改 a 标记内的 html,请使用以下命令:
var notificationUnread = document.getElementById('notificationUnread').innerHTML = "your html here";
否则,您只能通过将innerHTML 替换为innerText 来更改文本
- 3 回答
- 0 关注
- 162 浏览
添加回答
举报