<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
请输入关键词<input id="keyword" type="text" value="" onkeyup="sugesst()"/>
<ul id="list"> </ul>
<script>
//http://suggestion.baidu.com/su?wd='+keyword+'&cb=show
function show(allData) {
var data = allData.s;
/*var str='';
data.forEach(function (item) {
str+='<li>'+item+'</li>';
document.querySelector("#list").innerHTML=str;
});
if(document.querySelector("#keyword").value==""){
document.querySelector("#list").innerHTML="";
}*/
//上边太麻烦,用map,map本身有返回值
var strArr=data.map(function(item) {
return '<li>'+item+'</li>';
}).join();
document.querySelector("#list").innerHTML=strArr;
console.log(typeof strArr);
}
function sugesst() {
var script = document.createElement("script");
var keyword=document.querySelector("#keyword").value;
script.src = 'http://suggestion.baidu.com/su?wd=' + keyword + '&cb=show';
document.querySelector("body").appendChild(script);
}
</script>
</body>
</html>
05-11 07:31