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 關注
- 244 瀏覽
添加回答
舉報