jsonp_百度联想

<!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>
posted @ 2017-02-17 18:43  每天都要学进去一些  阅读(250)  评论(0编辑  收藏  举报