jsonp的使用
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><button id="btn">获取资源</button></body><script src="./untils.js"></script><script>var oBtn = document.getElementById('btn');oBtn.onclick = function () {xhr.ajax({url: 'http:://test.jsplusplus.com/jsonp/jsonp.php',type: 'get',dataType: 'JSONP',jsonp: 'cb',//jsonpCallback: 'test',success: function (data) {console.log(data);}})}</script></html>

实战百度联想词

实战百度联想词
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>百度联想词</title><link rel="stylesheet" href="css/index.css" /></head><body><div class="search-wrap"><div class="input-wrap"><input type="text" class="search-input J_searchInput" /></div><div class="list-wrap"><ul class="wd-list J_wdList"></ul></div></div><script type="text/html" id="J_listTpl"><li class="wd-item"><a href="https://www.baidu.com/s?wd={{wdLink}}" target="_blank" class="wd-lk">{{wd}}</a></li></script><script src="js/index.js"></script></body></html>
div, input, li{
box-sizing: border-box;
outline: none;
}
ul{
padding: 0;
margin: 0;
list-style: none;
}
a{
text-decoration: none;
}
.search-wrap{
position: relative;
width: 536px;
height: 40px;
margin: 200px auto;
}
.search-wrap .search-input{
width: 100%;
height: 40px;
border: 1px solid #b8b8b8;
border-bottom: 1px solid #ccc;
text-indent: 10px;
font-size: 18px;
}
.search-wrap .search-input:hover{
border: 1px solid #999;
}
.search-wrap .search-input:focus{
border: 1px solid #38f;
}
.search-wrap .list-wrap{
display: none;
position: absolute;
top: 40px;
left: 0;
width: 100%;
border: 1px solid #ccc;
background-color: #fff;
}
.search-wrap .list-wrap .wd-item{
height: 25px;
padding-left: 10px;
line-height: 25px;
}
.search-wrap .list-wrap .wd-item:hover{
background-color: #efefef;
}
.search-wrap .list-wrap .wd-lk{
font-weight: bold;
color: #000;
font-size: 14px;
}
.search-wrap .list-wrap .wd-lk .font-normal{
font-weight: normal;
}
;
(function (doc) {
var searchInput = doc.getElementsByClassName('J_searchInput')[0],
wdList = doc.getElementsByClassName('J_wdList')[0],
listWrap = wdList.parentNode,
firstScript = doc.getElementsByTagName('script')[0],
listTpl = doc.getElementById('J_listTpl').innerHTML;
var init = function () {
bindEvent();
}
function bindEvent() {
searchInput.addEventListener('input', typeInput, false);
}
function renderList(data) {
var list = '',
data = data.s,
len = data.length,
val = _trimSpace(searchInput.value);
if (len) {
data.forEach(function (elem) {
list += listTpl.replace(/{{(.*?)}}/g, function (node, key) {
return {
wdLink: elem,
wd: _setWdStyle(val, elem)
} [key];
});
});
wdList.innerHTML = list;
listWrap.style.display = 'block';
} else {
wdList.innerHTML = '';
listWrap.style.display = 'none';
}
}
function typeInput() {
// 去除输入数据的空格
var val = _trimSpace(searchInput.value);
if (val.length > 0) {
// getDatas(val,'setDatas');
getDatas(val);
} else {
wdList.innerHTML = '';
listWrap.style.display = 'none';
}
}
function getDatas(value) {
// var oScript = doc.createElement('script');
// oScript.src = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + value + '&cb=' + callbackName;
// doc.body.insertBefore(oScript, firstScript);
// doc.body.removeChild(oScript);
xhr.ajax({
url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + value,
type: 'GET',
dataType: 'JSONP',
jsonp: 'cb',
success: function (data) {
// console.log(data);
renderList(data);
}
});
}
// 去除输入数据的空格
function _trimSpace(str) {
return str.replace(/\s+/g, '');
}
function _setWdStyle(value, word) {
// 把输入的的字变细 //去除输入的字
return '<span class="font-normal">' + value + '</span>' + word.replace(value, '');
}
// window.setDatas = function (data) {
// console.log(data);
// renderList(data);
// }
init();
})(document);
