开发者

find and replace on a HTMLelement.INNERHTML?

 var e = document.getElementById('anelement');
 e.innerHTML = f.anelement开发者_运维知识库

inside anelement I'd like to replace every occurance of a certain word e.g apple with orange how would I go about doing this?


innerHTML is a String object which has the replace method:

e.innerHTML = f.anelement.innerHTML.replace("apple", "orange");

Edit

As Matt Brock pointed out in comments, a simple replace does not differentiate between whole words and substrings. A more precise solution, using regular expressions, would be:

var result = "apple to orange".replace(/\b(apple)\b/g, "orange");
// evaluates to "orange to orange"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜