Home > Web Front-end > JS Tutorial > body text

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

巴扎黑
Release: 2017-06-16 10:30:20
Original
1676 people have browsed it

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);//更新文章;
});
Copy after login

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);
    }
   });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template