亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 textContent 中附加 CSS 類?

如何在 textContent 中附加 CSS 類?

萬千封印 2024-01-11 10:38:44
您好,我正在嘗試更改以下 HTML 代碼 textContent,但是它包含一個<i>標簽,那么我如何更改 textContent?如何編輯腳本中的 HTML 標記以保留<i>標記類但僅更改其中的文本?這是默認 HTML 的樣子這是我編輯后的樣子HTML 代碼<a id="notificationUnread" class="dropdown-item">   <i class="fas fa-envelope mr-2"></i> 0 unread notifications   </a>腳本var notificationUnread = document.getElementById('notificationUnread').textContent = "TEST"
查看完整描述

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>


查看完整回答
反對 回復 2024-01-11
?
拉莫斯之舞

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"


查看完整回答
反對 回復 2024-01-11
?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

如果您想更改 a 標記內的 html,請使用以下命令:

var notificationUnread = document.getElementById('notificationUnread').innerHTML = "your html here";

否則,您只能通過將innerHTML 替換為innerText 來更改文本


查看完整回答
反對 回復 2024-01-11
  • 3 回答
  • 0 關注
  • 244 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號