admin管理员组文章数量:1401636
I'd like to change some text that is between those tags :
<div id=thing>
<a href=link></a>texttochangehere<a href=link2></a>
</div>
But without changing the links. I tried replacing only the part I want with innerHTML and substring, but it seems to unlink the tags that another script uses.
I'd like to change some text that is between those tags :
<div id=thing>
<a href=link></a>texttochangehere<a href=link2></a>
</div>
But without changing the links. I tried replacing only the part I want with innerHTML and substring, but it seems to unlink the tags that another script uses.
Share Improve this question asked Jun 25, 2013 at 15:56 keywckeywc 31 silver badge2 bronze badges 2-
textContent
vs.innerHTML
. See alsoinnerText
. – Teemu Commented Jun 25, 2013 at 15:58 - 1 stackoverflow./a/8118165/2256325 – Sergio Commented Jun 25, 2013 at 15:59
1 Answer
Reset to default 6There are at least two ways to achieve your goal:
- String replacement and HTML parsing (using
innerHTML
) - DOM manipulation setting the text node (using
textContent
)
var div = document.getElementById('thing');
// replace text in HTML string:
div.innerHTML = div.innerHTML.replace('texttochangehere','changedtext');
// manipulating text node:
for(var node of div.childNodes){
if(node.nodeType == 3 && node.textContent == 'texttochangehere')
node.textContent = 'changedtext';
}
本文标签: Change text between tags with javascriptStack Overflow
版权声明:本文标题:Change text between tags with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744281368a2598667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论