这是我的代码:
Html:<input onkeyup="showResult(this.value)" id="tsearch" class="search-query form-control" type="text" placeholder="Search here ..." name="tsearch"></input>
<div id="livesearch" style="border: 0px none;">
脚本:
<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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("livesearch").innerHTML = xmlhttp.responseText;
document.getElementById("livesearch").style.border = "1px solid #A5ACB2";
}
}
xmlhttp.open("GET", "livesearch.php?q=" + str, true);
xmlhttp.send();
}
</script>
一旦在搜索框中提供了输入,livessearch .php将返回结果。我必须对上面的代码进行哪些更改,以便在搜索结果上提供导航键支持,并在页面的任何其他区域按下鼠标后删除结果?
你可以把所有特定的搜索结果放在它们自己的div容器中,作为你#livesearchdiv的子div。
在keyup函数内部,使用函数的event参数来获取箭头上下的keycode:
- left = 37
- up = 38
- right = 39
- down = 40
然后使用另一个函数来聚焦所需的搜索结果。
希望这对你有帮助。