我正在尝试使用ajax进行自动完成搜索。我希望字符不需要出现不同的颜色(黑色)。
<script>
function showResult(str) {
if (str.length==0) {
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("livesearch").innerHTML=this.responseText;
document.getElementById("livesearch").style.border="1px solid #A5bCB2";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</body>
我的 livesearch.php 看起来像:
echo "Tirana";
echo "Tepelene";
echo "Korca";
echo "Skrapar";
例如:(照片)
你能帮帮我吗? 提前致谢!
最佳答案
尝试如下:
var pattern = "(^|\s)(" + str+ ")(\s|$)";
var regexp = new RegExp(pattern,"ig");
this.responseText.replace(regexp, '$1<b>$2</b>$3');
关于javascript - 自动完成:以不同的颜色显示未完成的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39485379/