我目前正在使用 .replace 函数来替换页面上的特定字符串。鉴于我不知道当前字符串的位置,所以我无法选择它,我的代码如下所示:
$('body').html( $('body').html().replace(regex, 'sometext') );
因此,如果页面最初看起来像这样:
<div class="a">Hello</div>
It now looks like this:
<div class="a">sometext</div>
有没有办法在不使用的情况下做到这一点$('body').html( $('body').html().replace() )
?
谢谢!
编辑:示例
<p class="last">Mac, iPhone, iPod and iPad customers within 90 days of ownership are eligible for complimentary phone support — one support incident per iPod and unlimited incidents per Mac, iPhone and iPad. <a href="/support/" onclick="s_objectID="http://www.apple.com/support/_3";return this.s_oc?this.s_oc(e):true">Online technical support</a> for Apple products is available beyond the initial 90 days.</p>
使用这个:
$('body').html( $('body').html().replace("iPhone", '<a href="#">iPhone</a>') );
它将替换 iPhone 的每个实例,使其看起来像 iPhone
导致:
<p class="last">Mac, <a href="#">iPhone</a>, iPod and iPad customers within 90 days of ownership are eligible for complimentary phone support — one support incident per iPod and unlimited incidents per Mac, <a href="#">iPhone</a> and iPad. <a href="/support/" onclick="s_objectID="http://www.apple.com/support/_3";return this.s_oc?this.s_oc(e):true">Online technical support</a> for Apple products is available beyond the initial 90 days.</p>