我有一个简单的 h2
元素,如下所示:
<h2>New Agent - Record Status:
@Html.DisplayFor(model => model.RecordDisplayStatus)
</h2>
当用户单击按钮时,我需要能够将其中的单词 Agent
更改为 Agency
。这是我的第一次尝试,但我不断收到以下消息:
Uncaught TypeError: Object [object Object] has no method 'replace'
这是我的第一个技巧
$('.head h2').replace("Agent", "Agency");
最佳答案
那是因为您尝试在 jQuery 对象上调用replace
。您需要对字符串调用 replace
,因此请尝试以下操作:
var newval = $('.head h2').text().replace("Agent", "Agency");
$('.head h2').text(newval);
还有一个fiddle
关于javascript - 替换 h2 元素中的特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295379/