我目前使用.replace函數替換頁面上的特定字符串。由於我不知道當前字符串所在,所以我不能選擇它,我的代碼看起來是這樣的:使用jQuery替換和正則表達式替換字符串
$('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>
的事情是,我試圖使它儘可能通用,所以它可以用於任何頁面。我用一個例子編輯了這篇文章。 – jsllsj 2012-04-26 23:20:15
所以你正在尋找一個更簡潔的方式來做到這一點? – 2012-04-26 23:31:14
我正在尋找一種方法來做到這一點,而無需更換整個dom。這似乎打破了很多頁面,其中有任何js。 – jsllsj 2012-04-26 23:37:49