This article mainly introduces the style replacement function of LI under UL based on regular expressions, and involves related implementation techniques of JavaScript using regular expressions to dynamically operate page element attributes. Friends in need can refer to the following
The example of this article describes the implementation of the style replacement function of LI under UL based on regular expressions. Share it with everyone for your reference, the details are as follows:
First I thought of filling it under UL, but then I found that the result was not satisfactory and the style was not really changed:
$("#UlContent li").each(function (index) { // alert(index + ': ' + $(this).text()); var text = $(this).text(); var regExp = new RegExp($("#search_content").val(), 'g'); var newText = text.replace(regExp,"<span style=\"background-color:red;\">" + $("#search_content").val() + "</span>");//将找到的关键字替换,加上highlight属性; $(this).text(newText);//更新文章; });
In fact, it should be replaced before filling in UL:
$("#search_content").keyup(function () { if(CheckChinese($("#search_content").val())) { $.ajax({ type: "POST", anync: true, url: "HelpCenterSuggestion.ashx", cache: false, dataType: "text", data: { m: $("#search_content").val() }, success: function (result) { $("#UlContent li").remove(); var regExp = new RegExp($("#search_content").val(), 'g'); var newText = result.replace(regExp,"<span style=\"background-color:red;\">" + $("#search_content").val() + "</span>");//将找到的关键字替换,加上highlight属性; $("#UlContent").append(newText); } });
The above is the detailed content of Implementing the style replacement function of LI under UL based on regular expressions. For more information, please follow other related articles on the PHP Chinese website!