百度搜索下拉框

<!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='';
        //将JSON形式的字符串转为数组
        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":"你是我的小苹果"}';
}
?>

猜你喜欢

转载自blog.csdn.net/wen15191038073/article/details/119820179