我有一个显示“香蕉”的字符串
我想把它换成“Apple”
这是我的
<span id="Fruit">Banana</span>
这是我正在使用的脚本。
var td1 = document.getElementById('Fruit');
var text = document.createTextNode("Apple");
td1.appendChild(text);
这就是它显示的样子。
香蕉苹果
<span id="Fruit">
"Banana"
"Apple"
</span>
我怎样才能摆脱 Banana 并用 Apple 取而代之?
<span id="Fruit">Apple</span>
最佳答案
只需更新 innerHTML:http://jsfiddle.net/SNtmL/
document.getElementById('Fruit').innerHTML = "Apple";
关于javascript - 如何在 HTML 元素中替换此文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18155976/