我正在尝试用JS替换我的HTML代码的一部分但是不起作用.
我有下一个代码:
<td id="one"><a href="servlet.jsp?account='old'" >old</a></td>
Run Code Online (Sandbox Code Playgroud)
我想改变"旧"为"新",像这样:
<td id="one"><a href="servlet.jsp?account='new'" >new</a></td>
Run Code Online (Sandbox Code Playgroud)
然后我使用JS,因为我需要使用方法replace(),然后,我的JS是:
var old = "old";
var rep = "new";
var elem = document.getElementById("one");
var text = elem.innerHTML;
text = text.replace(old,rep);
elem.innerHTML = text;
Run Code Online (Sandbox Code Playgroud)
我尝试搜索但总是建立相同的.
欢迎任何建议.
(我是通过记忆写出来的,因为我不在办公室而且它更复杂)我明天会尝试)
因为"new"是JavaScript中的保留字.
请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words