本文实例为大家分享了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="/UploadFiles/2021-04-02/google.png">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" + document.getElementById("searchData").value;
百度:https://m.baidu.com/s"external nofollow" href="https://www.google.com/search">https://www.google.com/search"external nofollow" href="https://cn.bing.com/search">https://cn.bing.com/search"external nofollow" href="https://m.zhihu.com/search">https://m.zhihu.com/search"external nofollow" href="http://wap.sogou.com/web/searchList.jsp">http://wap.sogou.com/web/searchList.jsp"external nofollow" href="http://so.m.jd.com/ware/search.action">http://so.m.jd.com/ware/search.action"htmlcode">
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(); } } },如有更好的方式欢迎讨论。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]