Home  >  Article  >  Web Front-end  >  Implementing the style replacement function of LI under UL based on regular expressions

Implementing the style replacement function of LI under UL based on regular expressions

巴扎黑
巴扎黑Original
2017-06-16 10:30:201659browse

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(), &#39;g&#39;);
      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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn