简体   繁体   English

突出显示搜索关键字

[英]Highlighting search keyword

I created a simple database and there is a live search,i want to know how to highlighting search keywords in the table.我创建了一个简单的数据库并且有一个实时搜索,我想知道如何在表格中突出显示搜索关键字。 I've been using other means but still it doesn't work.我一直在使用其他方法,但仍然不起作用。 please help me请帮我

this is my code:这是我的代码:

$(document).ready(function(){
    load_data();

    function load_data(search){
      $.ajax({
        url:"get_data.php",
        method:"POST",
        data: {
          search: search
        },
        success:function(data){
          $('#hasil').html(data);
        }
      });
    }
    $('#search').keyup(function(){
      var search = $("#search").val();
      load_data(search);
    });


    function highlight_word(hasil)
      {
          var text = document.getElementById("search").value;
          if(text)
          {
              var pattern=new RegExp("("+text+")", "gi");
              var new_text=hasil.replace(pattern, "<span class='highlight'>"+text+"</span>");
              document.getElementById("hasil").innerHTML=new_text; 
          }
      }

html: html:

  <input type="text" class="form-control" id="search" name="search" >
  <div id="hasil"></div>

You've never called highlight_word() function somewhere.您从未在某处调用过 highlight_word() function。 you may replace your success function via:您可以通过以下方式替换您的成功 function:

success:function(data){
      $('#hasil').html(data);
      highlight_word(data);
    }

Follow This Article.按照这篇文章。 You could this way你可以这样

[https://stackoverflow.com/questions/8644428/how-to-highlight-text-using-javascript][1] [https://stackoverflow.com/questions/8644428/how-to-highlight-text-using-javascript][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM