<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<title>百度搜索提示</title>
</head>
<body>
<input id="txt" placeholder="百度搜索提示" />
<ol id="list"></ol>
</body>
</html>
document.getElementById('txt').oninput = function () {
sug(this.value);
}
//发起JSONP
function sug(key) {
var ele = document.createElement("SCRIPT");
ele.src = "https://www.baidu.com/sugrec?prod=pc&cb=callback&wd=" + encodeURIComponent(key) + "&_" + Math.random();
document.getElementsByTagName("HEAD")[0].appendChild(ele);
}
//JSONP 回调
window["callback"] = (data) => {
var htm = [];
if (data.g) {
for (var i = 0; i < data.g.length; i++) {
htm.push('<li>' + data.g[i].q + '</li>')
}
}
document.getElementById("list").innerHTML = htm.join('')
}