Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何附加一个包含CSS类的textContent?_Javascript_Html_Css_Ajax_Dom - Fatal编程技术网

Javascript 如何附加一个包含CSS类的textContent?

Javascript 如何附加一个包含CSS类的textContent?,javascript,html,css,ajax,dom,Javascript,Html,Css,Ajax,Dom,您好,我正在尝试更改下面的HTML代码文本内容,但是其中包含标记,因此如何更改文本内容?如何编辑脚本中的HTML标记以保留标记类,但只更改其中的文本 这就是默认HTML的样子 这是我编辑后的样子 HTML代码 如果要更改a标记内的html,请使用以下命令: var notificationUnread = document.getElementById('notificationUnread').innerHTML = "your html here"; 否则,您只能通过将innerHT

您好,我正在尝试更改下面的HTML代码文本内容,但是其中包含
标记,因此如何更改文本内容?如何编辑脚本中的HTML标记以保留
标记类,但只更改其中的文本

这就是默认HTML的样子

这是我编辑后的样子

HTML代码



如果要更改a标记内的html,请使用以下命令:

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

否则,您只能通过将innerHTML替换为innerText来更改文本。您可以使用
lastChild
childNodes[lastIndex]
以最后一个文本节点为目标,并使用
node.nodeValue=“Something”

//let nodes=document.getElementById('notificationUnread').childNodes
//节点[nodes.length-1].nodeValue=“TES”
//无跨度
document.getElementById(“notificationUnread”).lastChild.nodeValue=“测试不带span”
//有跨度
document.querySelector(“#notificationUnread2>.text notification”).innerText=“使用span进行测试”

0个未读通知

0个未读通知
将要更改的文本包装到


您好,我已尝试使用.innerHTML,但这将删除标记,只留下“文本”。然后使用innerText属性-->var notificationUnread=document.getElementById('notificationUnread')。innerText=“此处的文本”;您好,我已经尝试了.innerText和.innerHTML,但是上面使用.lastChild.nodeValue的解决方案对我有效。谢谢你的帮助真的很感谢有时间帮忙。你好,谢谢你的帮助。但是我想问一下,如果它不是.firstChild或.lastChild,我怎么能纠正它呢?你应该用span包装文本,给出class或id,然后使用选择器来定位它并更改它。例如,我已经更新了答案,请用span和不带span检查它。好的,非常感谢你提供的详细信息解释我将看一看代码,并尝试理解它。
var notificationUnread = document.getElementById('notificationUnread').textContent = "TEST"
var notificationUnread = document.getElementById('notificationUnread').innerHTML = "your html here";
document.getElementById("notificationUnread").lastChild.nodeValue="Test"
 let nodes=document.getElementById('notificationUnread').childNodes
 nodes[nodes.length-1].nodeValue= "TES"
<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"