开发者

Find and Replace Text with Prototype

I am new to Prototype and was wondering how to simply "find" some text in a class/ID and "replace" this text.

My html is like this:

<div id="test">
  <a href="/test">test</a>
  <a href="/test2">test2</a>
  <a href="/test3">test3</a>
  <a href="/test4">test4</a>
</div>

And I am trying to replace the "test" in between the <a> tags for the first <a> with the word "success" and the second <a> with "success2". So it would look like

<div id="test">
      <a href="/test">Login</a>
      <a href="/test2">Register</a>
      <a href="/test3">Bla</a>
      <a href="/test4开发者_Go百科">Logout</a>
    </div>

Any ideas how to do this ?


Here is a different approach using getElementsByTagName

var r=[['Login','success'],['Register','donkey'],['Bla','...'],['Logout','Are you sure?']];
var x=document.getElementById("test");
var a=x.getElementsByTagName("a");
for(i in a){
    a[i].innerHTML=a[i].innerHTML.replace(r[i][0],r[i][1]);
}


With DOM

var x=document.getElementById("test");
for(var i in x.childNodes){
    if(x.childNodes[i].tagName=='A'){
        x.childNodes[i].innerHTML=x.childNodes[i].innerHTML.replace(/test/,'success');
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜