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';
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744281368a4566585.html
评论列表(0条)