【发布时间】:2014-02-18 02:12:46
【问题描述】:
我有这个 html:
<div id="content">This following word is not <span id="notOk">ok</span> but all the other words are ok</div>
使用这个 jquery,我试图用 cool 替换单词 ok,只要单词 ok 不在跨度 #notOk 内。
var content = $('#content').html()
content = content.replace('ok', 'cool');
$('#content').html(content)
我也想保留句子,不移动任何单词,这就是我尝试时发生的情况。我想我正在寻找类似 dontGetElementByID('').?
【问题讨论】:
-
没有得到你想说的。
-
你的意思是说如果'ok'这个词在 Id=#notOk 的范围内,你希望它被替换为'cool'?其他“好的”不应该被替换。我说的对吗?
-
你想做什么??更改跨度内的第一个 ok,跨度外的 ok,还是两者都更改?
-
替换后,我希望 html 为
This following word is not <span id="notOk">ok</span> but all the other words are cool</div>而不是This following word is not <span id="notOk">cool</span> but all the other words are ok</div>,这是正在发生的事情。这更清楚吗? -
@user2891084 更改跨度外的
ok
标签: javascript jquery replace