搜索关键字描红
使用JavaScript对处理关键字对后台数据库进行搜索时,对得到的搜索记录中的匹配关键字进行描红处理: 1: $.getJSON("http://10.65.9.187:8090/MySQLdemo/servlet/SearchControl", {"keyword":keyword}, function(data) { 2: //alert(data[1].bookname); 3: $.each(data, function(index, item){ 4: str += "<tr><td>"+item.id+"</td><td>"+item.bookname+"</td><td>"+item.issuDate+"</td><td>"+item.price+"</td><td>"+item.synopsis+"</td><td>"+item.Maker+"</td><td>"+item.pulisher+"</td></tr>"; 5: }); 6: str = str.replace(new RegExp(keyword, "gi"), "<font color='red'>"+keyword+"</font>"); 7: $("#mytb").html(str); 8: 9: }); 在这里有一个问题需要注意:使用 string .replace( regexp ,