小编给大家分享一下vue怎么实现多引擎搜索及关键字提示,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以创建可维护性和可测试性更强的代码库,Vue允许可以将一个网页分割成可复用的组件,每个组件都包含属于自己的HTML、CSS、JavaScript,以用来渲染网页中相应的地方,所以越来越多的前端开发者使用vue。
具体内容如下
关键代码:
<div class="header-search">
<form id="form" action="http://m.baidu.com/s" method="get" accept-charset="utf-8" class="clearfix" autocomplete="off">
<a>
<span class="search-media"></span>
</a>
<input id="searchData" type="text" placeholder="搜索一下" name="word" @keyup="listenWords" @input="listenInput" @focus="listenInput" />
<span class="del">×</span>
<a @click="gotoSearch">
<span class="icon-search icon-sign"></span>
</a>
</form>
</div>
<div id="pagesZone" class="clearfix">
<div id="auto"></div>
<span class="engi">快速搜索:</span>
<img src="../../dist/images/google.png" alt="谷歌" id="googlePages" @click="gotoGoogle" >
<img src="../../dist/images/bing.png" alt="必应" id="bing" @click="gotoBing" >
<img src="../../dist/images/zhihu.png" alt="知乎" id="zhihu" @click="gotoZhiHu" >
<img src="../../dist/images/sogou.png" alt="搜狗" id="sogo" @click="gotoSogou" >
<img src="../../dist/images/jd.png" alt="京东" id="jd" @click="gotoJD" >
<a @click="close" class="close">关闭</a>
</div>
fillUrls: function() {
var that = this;
var strdomin = document.getElementById("searchData").value;
window.status = "请求中";
this.$http.jsonp("http://suggestion.baidu.com/su", { //请求参数
params: {
wd: strdomin
},
jsonp: 'cb'
}).then(function(res){
window.status = "请求结束";
that.autoDisplay(JSON.parse(res.body).s);
},function(){
console.log("error");
});
},
autoDisplay: function(autoStr) {
var searchText = document.getElementById('searchData');
var autoNode = document.getElementById('auto'); //缓存对象(弹出框)
var that = this;
var docWidth = document.body.clientWidth || document.documentElement.clientWidth;
var pagesZone = document.getElementById('pagesZone');
if (autoStr.length == 0) {
console.log("false");
autoNode.style.display = "none";
return false;
}
autoNode.innerHTML = "";
for (var i = 0; i < autoStr.length; i++) {
//创建节点
var wordNode = autoStr[i].replace(searchText.value,"<b>"+searchText.value+"</b>");
var newDivNode = document.createElement('div');
newDivNode.setAttribute("id",i);
autoNode.appendChild(newDivNode);
var wordSpanNode = document.createElement('span');
wordSpanNode.setAttribute('class','suggText');
wordSpanNode.innerHTML = wordNode;
newDivNode.appendChild(wordSpanNode);
var addNode = document.createElement('span');
addNode.setAttribute('class','addText');
addNode.innerHTML = '+';
newDivNode.appendChild(addNode);
//鼠标点击文字上屏并搜索
wordSpanNode.onclick = function () {
this.highlightindex = this.parentNode.getAttribute('id');
var comText = autoNode.childNodes[this.highlightindex].firstChild.innerText;
autoNode.style.display = "none";
this.highlightindex = -1;
searchText.value = comText;
pagesZone.style.display = "none";
that.gotoSearch();
};
//鼠标点击文字上屏
addNode.onclick = function () {
this.highlightindex = this.parentNode.getAttribute('id');
var comText = autoNode.childNodes[this.highlightindex].firstChild.innerText;
autoNode.style.display = "none";
this.highlightindex = -1;
searchText.value = comText;
};
//展示
if (autoStr.length > 0) {
autoNode.style.display = "block";
} else {
autoNode.style.display = "none";
this.highlightindex = -1;
}
//针对手机竖屏时的显示条数控制
if (docWidth < 500 && i > 3) {
break;
}
}
},
close: function() {
document.getElementById('pagesZone').style.display = 'none';
},
listenWords: function(event) {
console.log("listen keyup");
var that = this;
var searchInput = document.getElementById("searchData");
event = window.event || event;
if (event.keyCode == 13) { // enter
event.preventDefault();
that.gotoSearch();
}
if (event.keyCode == 8) { // backspace
console.log(searchInput.value.length);
if(searchInput.value.length == 0){
searchInput.blur();
searchInput.focus();
}
}
},
listenInput: function() {
var that = this;
var searchInput = document.getElementById("searchData");
var auto = document.getElementById('auto');
var pagesZone = document.getElementById('pagesZone');
var del = document.getElementsByClassName('del')[0];
if (searchInput.value == null || searchInput.value == "") {
auto.innerHTML = "";
pagesZone.style.display = "none";
del.style.display = "none";
auto.style.display = "none";
return;
}
pagesZone.style.display = "block";
del.style.display = "block";
that.fillUrls();
if (this.highlightindex != -1) {
this.highlightindex = -1;
}
},
多引擎搜索很简单,匹配对应参数就好:
window.location.href = "https://m.zhihu.com/search?q=" + document.getElementById("searchData").value;
百度:https://m.baidu.com/s?word=
谷歌:https://www.google.com/search?q=
必应:https://cn.bing.com/search?q=
知乎:https://m.zhihu.com/search?q=
搜狗:http://wap.sogou.com/web/searchList.jsp?keyword=
京东:http://so.m.jd.com/ware/search.action?keyword=
关键字提示,先通过jsonp请求参数:
var strdomin = document.getElementById("searchData").value;
window.status = "请求中";
this.$http.jsonp("http://suggestion.baidu.com/su", { //请求参数
params: {
wd: strdomin
},
jsonp: 'cb'
}).then(function(res){
window.status = "请求结束";
that.autoDisplay(JSON.parse(res.body).s);
},function(){
console.log("error");
});
输入框中有文字的时候触发。
其中JSON.parse用于从一个字符串中解析出json对象。s是suggest words。这里传到autoDisplay的参数即关键字提示。
另外将input元素的autocomplete属性设置为off可以关闭自动提示:
<input type="text" name="name" autocomplete="off">
如果所有表单元素都不想使用自动提示功能,只需在表单form上设置autocomplete=off。
最后将获取到的关键字提示放到input下面的节点中即可。
注意:
复制代码 代码如下:
<input id="searchData" type="text" placeholder="搜索一下" name="word" @keyup="listenWords" @input="listenInput" @focus="listenInput" />
这里因兼容问题绑定了3个事件,其中listenWords专门针对手机键盘的回车键和回退键:
listenWords: function(event) {
console.log("listen keyup");
var that = this;
var searchInput = document.getElementById("searchData");
event = window.event || event;
if (event.keyCode == 13) { // enter
event.preventDefault();
that.gotoSearch();
}
if (event.keyCode == 8) { // backspace
console.log(searchInput.value.length);
if(searchInput.value.length == 0){
searchInput.blur();
searchInput.focus();
}
}
},
以上是“vue怎么实现多引擎搜索及关键字提示”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。