我創建了一個簡單的資料庫并且有一個實時搜索,我想知道如何在表格中突出顯示搜索關鍵字。我一直在使用其他方法,但它仍然不起作用。請幫我
這是我的代碼:
$(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:
<input type="text" class="form-control" id="search" name="search" >
<div id="hasil"></div>
uj5u.com熱心網友回復:
您從未在某處呼叫 highlight_word() 函式。您可以通過以下方式替換您的成功功能:
success:function(data){
$('#hasil').html(data);
highlight_word(data);
}
uj5u.com熱心網友回復:
關注這篇文章。你可以這樣
[https://stackoverflow.com/questions/8644428/how-to-highlight-text-using-javascript][1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/443821.html
標籤:javascript php html 阿贾克斯
上一篇:此集合實體上不存在屬性[組]
下一篇:呼叫AJAX錯誤函式而不是成功
