最近无聊写了个简单的关键词搜索代码,啧啧啧
实现的效果:
用到的工具代码:
/**
* 公用方法
*/
var utils={
//获取滚动条的高度
getScrollTop:function(){let scrollTop=0;if(document.documentElement&&document.documentElement.scrollTop){scrollTop=document.documentElement.scrollTop;}else if(document.body) {scrollTop=document.body.scrollTop;}return scrollTop;},
//获取标签
ele:function(text){if(text!=null&text!=""){if(text.length>1){if (/^#[a-zA-Z0-9_]*$/.test(text)){return document.getElementById(text.substring(1,text.length));}else if(/^\.[a-zA-Z0-9_]*$/.test(text)){return document.getElementsByClassName(text.substring(1,text.length));}else{return document.getElementsByTagName(text);}}}return null;},
//设置文本框的最大输入长度
inputTextLength:function(ele,textLength){let py=false;ele.addEventListener("input",function(){if(!py){sub();}});ele.addEventListener("compositionstart",function(){py=true;});ele.addEventListener("compositionend",function(){py=false;sub();});function sub(){let text=ele.value;if(text.length>=length){ele.value=text.substring(0,length);}}},
//设置css
addCss:function(el,cssStyles){if(typeof el=="object"){let css={};let oldCss=el.getAttribute("style");if(oldCss!=null){let oldCsss=oldCss.split(";");for(let i=0;i<oldCsss.length;i++){let keyAndValue=oldCsss[i];if(keyAndValue!=""){let k=keyAndValue.substring(0,keyAndValue.indexOf(":"));let v=keyAndValue.substring(keyAndValue.indexOf(":")+1,keyAndValue.length);css[k]=v.trim();}}}for(cssStyle in cssStyles){css[cssStyle]=cssStyles[cssStyle];}if(JSON.stringify(css)!="{}"){oldCss = "";for(let c in css){oldCss+=c+":"+css[c] +";";}}el.setAttribute("style",oldCss);}},
//创建标签
createElement:function (eleName,attrs,write,parseEle){if(typeof eleName=="string"){let ele=document.createElement(eleName);if(typeof attrs=="object"&attrs!=null){for(let attr in attrs){ele.setAttribute(attr, attrs[attr]);}}if(typeof write=="object"&write!=null){for(let text in write){if(text=="text"){this.InnerText(ele, write[text]);}else if(text=="html"){ele.innerHTML = write[text];}else if(text=="value"){ele.value = write[text];}}}if(typeof parseEle=="object"&parseEle!=null){parseEle.appendChild(ele);}return ele;}},
//分割数组
splitArray:function (array, arrayLength){let VarTempArray = [];let temp = [];for(let i=0;i<array.length;i++){temp.push(array[i]);if(i%arrayLength==(arrayLength-1)||i==(array.length-1)){VarTempArray.push(temp);temp = [];}}return VarTempArray;},
//时间格式化
dateTimeFormat:function(timeStamp,timeFormat){let date=new Date(timeStamp);let y=date.getFullYear();let M=(date.getMonth()+1<10?'0'+(date.getMonth()+1):date.getMonth()+1);let d=date.getDate()<10?'0'+date.getDate()+'':date.getDate()+'';let h=date.getHours()<10?'0'+date.getHours():date.getHours();let m=date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes();let s=date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds();if(timeFormat.indexOf("yyyy")!=-1||timeFormat.indexOf("YYYY")!=-1){timeFormat=timeFormat.replace(new RegExp(/yyyy|YYYY/g),y);}if(timeFormat.indexOf("MM")!=-1){timeFormat=timeFormat.replace(new RegExp(/MM/g),M);}if(timeFormat.indexOf("dd")!=-1||timeFormat.indexOf("DD")!=-1){timeFormat=timeFormat.replace(new RegExp(/dd|DD/g),d);}if(timeFormat.indexOf("hh")!=-1||timeFormat.indexOf("HH")!=-1){timeFormat=timeFormat.replace(new RegExp(/hh|HH/g),h);}if(timeFormat.indexOf("mm")!=-1){timeFormat=timeFormat.replace(new RegExp(/mm/g), m);}if(timeFormat.indexOf("ss")!=-1||timeFormat.indexOf("SS")!=-1){