最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript to replace innerText and not if its an attribute - Stack Overflow

programmeradmin4浏览0评论

How to use Javascript to selectively replace the word called Rindan in he below text , but not if its in some attribute like img alt=="Rindan" I want to replace the word Rindans which is an inner text and not if it is in attribute .

`"<a href="/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-rindan-family-syrian">Rindan's family on her legacy</a> &nbsp;<a href="/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-Rindan-family-syrian" target=""><img alt="Rindan's family on her legacy" border="0" class="cnnVideoIcon" height="10" src="/.e/img/3.0/global/icons/video_icon.gif" width="16" /></a>`"

How to use Javascript to selectively replace the word called Rindan in he below text , but not if its in some attribute like img alt=="Rindan" I want to replace the word Rindans which is an inner text and not if it is in attribute .

`"<a href="http://edition.cnn./video/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-rindan-family-syria.cnn">Rindan's family on her legacy</a> &nbsp;<a href="http://edition.cnn./video/?hpt=hp_t1#/video/bestoftv/2012/02/28/ac-marie-Rindan-family-syria.cnn" target=""><img alt="Rindan's family on her legacy" border="0" class="cnnVideoIcon" height="10" src="http://i.cdn.turner./cnn/.e/img/3.0/global/icons/video_icon.gif" width="16" /></a>`"
Share Improve this question asked Feb 28, 2012 at 7:36 NishantNishant 22k20 gold badges78 silver badges106 bronze badges 2
  • I need to use JS to parse a big HTML content and replace innerHtml's with some other data ..but if it is in attribute it should remain untouched .Further I will not be replacing with another name , but with a <span class="spellcheck_error">word</span> . I am looking for an approach like htmlcontent = some string . Use htmlparser on this and replace only the textual contens . – Nishant Commented Feb 28, 2012 at 7:51
  • Input - HTML CONTENT as a text ------> Output replace spellcheck error words within a span class -----> ( Ofcourse I already have an array of words with erros ] . If the word is in some attribute just ignore it and replace only if its a text . – Nishant Commented Feb 28, 2012 at 7:58
Add a ment  | 

4 Answers 4

Reset to default 1

You can use the function parameter in replace function of string

your_string = your_string.replace(<regexp>,  
     function(matched, index){
        // check matched 
        return replacement;
     }); 
}); 

ref: https://developer.mozilla/en/JavaScript/Reference/Global_Objects/String/replace

Give the id to a block, get the innerHTML, and use the replace string function:

var get=document.getElementById('test');
var str=get.innerHTML;
document.write(str.replace("Rindan's","TestNAme"));

set this back to innerHTML

see live demo on http://jsfiddle/kunalvashist/CeeDU/

this is to replace the text and not the attributes:

var aa = document.getElementsByTagName('a');
for(i=0;i<=aa.length;i++)
{
aa[i].innerText = aa[i].innerText.replace("Rindan","anyone");
}
​

You could loop all children elements of the body tag and do a replace on the innerhtml.

var body_children = document.body.children;
for(var i = 0; i < body_children.length; i++){
   var str = body_children[i].innerHTML
   body_children[i].innerHTML = str.replace("this","with this");
}
发布评论

评论列表(0)

  1. 暂无评论