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"
精彩评论