3 回答

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

TA貢獻1851條經驗 獲得超5個贊
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);**
添加回答
舉報