<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=
, initial-scale=1.0">
<title>Document</title>
</head>
<body>
搜索:<input type="text"><br>
<ul>
</ul>
</body>
</html>
<script>
let oInput=document.querySelector('input');
let oUl=document.querySelector('ul');
oInput.oninput=function (){
if(this.value != ""){
let xhr=new XMLHttpRequest();
xhr.open('GET','sousuo.php?wd='+this.value,true);
xhr.send();
xhr.onreadystatechange=function (){
if(xhr.readyState==4 && xhr.status==200){
fun(xhr.responseText);
console.log(xhr.responseText);
}
}
}else{
oUl.innerHTML = "";
}
}
function fun(resText){
let strHtml='';
let obj=JSON.parse(resText);
for(let index in obj){
strHtml += `<li>${
obj[index]}</li>`;
}
oUl.innerHTML = strHtml;
}
</script>
<?php
header("Content-type:text/html;charset=utf-8");
$wd=$_GET['wd'];
if($wd=='你'){
echo '{"1":"你到底有没有爱过我","2":"你是我的眼","3":"你是我的小苹果"}';
}else if($wd=='我'){
echo '{"1":"你到底有没有爱过我","2":"你是我的眼","3":"你是我的小苹果"}';
}
?>