3 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
正則表達(dá)式和replace()思想。就像是
var text = $([selector]).html();
text = text.replace(/Now/g,'<strong>Now<\strong>');
$([selector]).html(text);
使用html()此操作時(shí)要小心。首先,有可能替換元素href屬性中的匹配字符串<a>以及其他可能導(dǎo)致頁面無法正常運(yùn)行的屬性??赡芸梢跃帉懜玫恼齽t表達(dá)式來克服某些潛在的問題,但是性能可能會(huì)受到影響(我不是正則表達(dá)式專家)。其次,html()用于替換內(nèi)容將導(dǎo)致不可序列化的數(shù)據(jù)(例如綁定到要替換的元素標(biāo)記的事件處理程序,表單數(shù)據(jù)等)丟失。編寫僅針對(duì)文本節(jié)點(diǎn)的函數(shù)可能是更好/更安全的選擇,這僅取決于頁面的復(fù)雜程度。
如果您有權(quán)訪問HMTL文件,則如果內(nèi)容是靜態(tài)的,則最好查找并替換要更改文件外觀的單詞。在大多數(shù)情況下,NotePad ++的 “ 在文件中查找”選項(xiàng)可以勝任此工作。
與SingleShot的建議一起使用并<span>與CSS類一起使用將比使用<strong>元素提供更多的靈活性。

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊
var Run=Run || {};
Run.makestrong= function(hoo, Rx){
if(hoo.data){
var X= document.createElement('strong');
X.style.color= 'red'; // testing only, easier to spot changes
var pa= hoo.parentNode;
var res, el, tem;
var str= hoo.data;
while(str && (res= Rx.exec(str))!= null){
var tem= res[1];
el= X.cloneNode(true);
el.appendChild(document.createTextNode(tem));
hoo.replaceData(res.index, tem.length,'');
hoo= hoo.splitText(res.index);
str= hoo.data;
if(str) pa.insertBefore(el, hoo);
else{
pa.appendChild(el);
return;
}
}
}
}
Run.godeep= function(hoo, fun, arg){
var A= [];
if(hoo){
hoo= hoo.firstChild;
while(hoo!= null){
if(hoo.nodeType== 3){
if(hoo.data) A[A.length]= fun(hoo, arg);
}
else A= A.concat(arguments.callee(hoo, fun, arg));
hoo= hoo.nextSibling;
}
}
return A;
}
//test
**Run.godeep(document.body, Run.makestrong,/([Ee]+)/g);**
添加回答
舉報(bào)