先看一下效果圖
在js中利用該函數進行獲取class為finKey的標簽並對其內容進行關鍵字的轉換,重寫在HTML中。
/** * 將關鍵字變為紅色 * @param keyWord 關鍵字字符串 * @returns .finKey class類為finKey */ function changeKeyRed(keyword) { if (keyword!=''){ $(".finKey").each(function () { var str=$(this).text(); var substr="/("+keyword+")/g"; var replaceStr=str.replace(eval(substr),"<span style='color:red;font-weight:bold'>"+keyword+"</span>") $(this).html(replaceStr); }); } }