/** * 处理新闻列表关键词高亮
* newsList-新闻列表数组 searchValue-搜索词,即需要高亮的字符 */ function setHighlight(newsList) { if (searchValue != '') { var arr = newsList; var newArr = []; var s = searchValue; var reg = new RegExp("(" + s + ")", "g"); console.log(s); for(var i =0; i< arr.length; i++){ var newstr = arr[i].TitleCN.replace(reg, "<span style='color:red'>$1</span>"); newArr.push({ TitleCN: newstr, SourceUrl: arr[i].SourceUrl }); } var htmlStr = ''; for(var i=0; i<newArr.length; i++) { htmlStr += '<p class="result-news-p"><i class="result-news-circle"></i><a class="result-news-one" href="'+newArr[i].SourceUrl+'" target="_blank">'+newArr[i].TitleCN+'</a></p>'; } $('#result-news-list').html(htmlStr); } }