收藏,日常必备的JS工具函数大全
Vue中文社区 昨天
为元素添加on方法
.. ..;.. (event, fn) {、 [][].(this, (el) { el.(event, fn); }); this;};
为元素添加trigger方法
.. (type, data) { event document.(); event.(type, , ); event. data {}; event. type; event. this; this.(event); this;};.. (event) { [][].(this, (el) { el[](event); }); this;};
转义html标签
(text) { text .(, ) .(, ) .(, ) .(, );}
HTML标签转义
(templateData) { s templateData[]; ( i ; i arguments.; i) { arg (arguments[i]); s arg .(, ) .(, ) .(, ); s templateData[i]; } s;} html ;
跨浏览器绑定事件
(obj, evt, fn) { (!oTarget) { ; } (obj.) { obj.(evt, fn, ); } (obj.) { obj.( evt, fn); } { oTarget[ sEvtType] fn; }}
加入收藏夹
(sURL, sTitle) { { window..(sURL, sTitle); } (e) { { window..(sTitle, sURL, ); } (e) { (); } }}
提取页面代码中所有网址
aa document.. .( ) .() .(, );(aa);
动态加载脚本文件
(src, text, reload, charset) { id (src text); (!reload (id, evalscripts)) ; (reload (id)) { (id)..((id)); } evalscripts.(id); scriptNode document.(); scriptNode. ; scriptNode. id; scriptNode. charset ? charset : . ? document. : document.; { (src) { scriptNode. src; scriptNode. ; scriptNode. () { scriptNode. ; [src] ; }; scriptNode. () { ( (scriptNode. scriptNode. ) !scriptNode. ) { scriptNode. ; [src] ; } }; } (text) { scriptNode. text; } document.()[].(scriptNode); } (e) {}}
返回顶部的通用方法
(btnId) { btn document.(btnId); d document.; b document.; window. set; btn.. ; btn. () { btn.. ; window. null; this. (() { d. .((d. b.) * ); b. .((d. b.) * ); (d. b. ) (btn., (window. set)); }, ); }; () { btn.. d. b. ? : ; }}();
实现base64解码
(data) { b64 ; o1, o2, o3, h1, h2, h3, h4, bits, i , ac , dec , tmp_arr []; (!data) { data; } data ; { h1 b64.(data.(i)); h2 b64.(data.(i)); h3 b64.(data.(i)); h4 b64.(data.(i)); bits (h1 ) | (h2 ) | (h3 ) | h4; o1 (bits ) & ; o2 (bits ) & ; o3 bits & ; (h3 ) { tmp_arr[ac] .(o1); } (h4 ) { tmp_arr[ac] .(o1, o2); } { tmp_arr[ac] .(o1, o2, o3); } } (i data.); dec tmp_arr.(); dec (dec); dec;}
确认是否是键盘有效输入值
(iKey) { (iKey iKey ) { ; } (iKey iKey ) { ; } (iKey iKey ) { ; } (iKey iKey ) { ; } (iKey iKey ) { ; } (iKey iKey ) { ; } (iKey iKey ) { ; } ;}
全角半角转换
(sStr, iCase) { ( sStr != sStr. <= !(iCase iCase ) ) { sStr; } i, oRs [], iCode; (iCase) { (i ; i sStr.; i ) { iCode sStr.(i); (iCode ) { iCode ; } (iCode ) { iCode ; } oRs.(.(iCode)); } } { (i ; i sStr.; i ) { iCode sStr.(i); (iCode ) { iCode ; } (iCode iCode ) { iCode ; } oRs.(.(iCode)); } } oRs.();}
版本对比
(v1, v2) { v1 v1.(); v2 v2.(); len .(v1., v2.); (v1. len) { v1.(); } (v2. len) { v2.(); } ( i ; i len; i) { num1 (v1[i]); num2 (v2[i]); (num1 num2) { ; } (num1 num2) { ; } } ;}
压缩CSS样式代码
(s) { s s.(, ); s s.(, ); s s.(, ); s s.(, ); s s.(); s null ? : s[];}
获取当前路径
currentPageUrl ; ( this. ) { currentPageUrl document..().();} { currentPageUrl this..().();}
字符串长度截取
(str, len) { temp, icount , patrn , strre ; ( i ; i str.; i) { (icount len ) { temp str.(i, ); (patrn.(temp) null) { icount icount } { icount icount } strre temp } { ; } } strre }
时间日期格式转换
.. (formatStr) { str formatStr; [, , , , , , ]; str str.(, this.()); str str.( , this.() % ? (this.() % ).() : (this.() % ) ); str str.( , this.() ? (this.() ).() : (this.() ) ); str str.(, this.() ); str str.(, [this.()]); str str.( , this.() ? this.().() : this.() ); str str.(, this.()); str str.( , this.() ? this.().() : this.() ); str str.(, this.()); str str.( , this.() ? this.().() : this.() ); str str.(, this.()); str str.( , this.() ? this.().() : this.() ); str str.(, this.()); str;};.. (format) { o { : this.() , : this.(), : this.(), : this.(), : this.(), : .((this.() ) / ), : this.() }; (.(format)) format format.( ., (this.() ).( ..) ); ( k o) { ( ( k ).(format)) format format.( ., .. ? o[k] : ( o[k]).(( o[k]).) ); } format;};( ().());
跨浏览器删除事件
(obj, evt, fn) { (!obj) { ; } (obj.) { obj.(evt, fn, ); } (oTarget.) { obj.( evt, fn); } { obj[ evt] fn; }}
判断是否以某个字符串结束
.. (s) { d this. s.; d >= this.(s) d;};
返回脚本内容
(s) { (s.() ) s; p ; arr []; ((arr p.(s))) { p1 ; arr1 []; arr1 p1.(arr[]); (arr1) { (arr1[], , arr1[], arr1[]); } { p1 ; arr1 p1.(arr[]); (, arr1[], arr1[].() != ); } } s;}
格式化CSS样式代码
(s) { s s.(, ); s s.(, ); s s.(, ); s s.(, ); s s.(, ); s s.(, ); s;}
获取cookie值
(name) { arr document..( ( name )); (arr != null) (arr[]); null;}
获得URL中GET参数值
() { querystr window...(); (querystr[]) { querystr[].(); []; (i ; i .; i) { tmp_arr .(); key tmp_arr[]; [key] tmp_arr[]; } } querystr[];}
获取移动设备初始化大小
() { (!this.) { screenWidth .(screen., screen.); (this.() !this.()) { screenWidth screenWidth / window.; } this. screenWidth / document..; } this.;}
获取页面高度
() { g document, a g., f g., d g. ? a : g.; .(f., a., d.);}
获取页面scrollLeft
() { a document; a.. a..;}
获取页面scrollTop
() { a document; a.. a..;}
获取页面可视高度
() { d document, a d. ? d. : d.; a.;}
获取页面可视宽度
() { d document, a d. ? d. : d.; a.;}
获取页面宽度
() { g document, a g., f g., d g. ? a : g.; .(f., a., d.);}
获取移动设备屏幕宽度
() { smallerSide .(screen., screen.); fixViewPortsExperiment rendererModel.. rendererModel..; fixViewPortsExperimentRunning fixViewPortsExperiment fixViewPortsExperiment.() ; (fixViewPortsExperiment) { (this.() !this.()) { smallerSide smallerSide / window.; } } smallerSide;}
获取网页被卷去的位置
() { document.. ? { : document.., : document.. } : { : document.., : document.. };}
获取URL上的参数
(name, type ) { newName name, reg ( newName , ), paramHash window...()[] , paramSearch window...()[] , param; type ? (param paramHash) : (param paramSearch); result param.(reg); (result != null) { result[].()[]; } null;}
检验URL链接是否有效
() { xmlhttp (); xmlhttp.(, , ); { xmlhttp.(); } (e) { } { result xmlhttp.; (result) { (xmlhttp. ) { ; } { ; } } { ; } }}
获取窗体可见范围的宽与高
() { de document.; db document.; viewW de. ? db. : de.; viewH de. ? db. : de.; (viewW, viewH);}
获取移动设备最大化大小
() { screenWidth .(window.) ? .(screen., screen.) : .(screen., screen.); (this.() !this.()) { screenWidth screenWidth / window.; } rendererModel.. rendererModel..; ( ); () { screenWidth / window.; } { screenWidth / document..; }}
判断是否安卓移动设备访问
() { .(navigator..());}
判断是否苹果移动设备访问
() { .(navigator..());}
判断是否为数字类型
(value) { patrn ; (patrn.(value) null value ) { ; } { ; }}
是否是某类手机型号
() { xSeriesConfig [ { : , : , : }, { : , : , : }, { : , : , : } ]; ( window !== window) { isIOS .(window..); (!isIOS) ; { devicePixelRatio, screen } window; { width, height } screen; xSeriesConfig.( item item. devicePixelRatio item. width item. height ); } ;};
判断是否移动设备
() { ( this. ) { this.; } screenWidth this.(); fixViewPortsExperiment rendererModel.. rendererModel..; fixViewPortsExperimentRunning fixViewPortsExperiment fixViewPortsExperiment.() ; (!fixViewPortsExperiment) { (!this.()) { screenWidth screenWidth / window.; } } isMobileScreenSize screenWidth ; isMobileUserAgent ; this. isMobileScreenSize this.(); this.;}
判断吗是否手机号码
(e) { i , n , a , o e , r o.(, ), d o.(, ), s !!.(o) (n.(r) >= ? : a.(r) >= ? : d ? : i.(r) >= ? : ); s;}
判断是否是移动设备访问
() { .( window...() );}
判断鼠标是否移出事件
(e, handler) { (e. !== ) { ; } reltg e. ? e. : e. ? e. : e.; (reltg reltg !== handler) { reltg reltg.; } reltg !== handler;}
判断是否Touch屏幕
() { ( window (window. document ) );}
判断是否为网址
(strUrl) { regular ; (regular.(strUrl)) { ; } { ; }}
判断是否打开视窗
() { !!document.();}
加载样式文件
(url) { { document.(url); } (e) { cssLink document.(); cssLink. ; cssLink. ; cssLink. url; head document.()[]; head.(cssLink); }}
替换地址栏
(url) { (history.) { history.(null, document., url); history.(); } { location.(url); }}
解决offsetX兼容性问题
(e) { target e., eventCoord, pageCoord, offsetCoord; pageCoord (target); eventCoord { : window. e., : window. e. }; offsetCoord { : eventCoord. pageCoord., : eventCoord. pageCoord. }; offsetCoord;} (element) { coord { : , : }; (element) { coord. element.; coord. element.; element element.; } coord;}
打开一个窗体通用方法
(url, windowName, width, height) { x (screen. / ) width / ; y (screen. / ) height / ; isMSIE navigator. ; (isMSIE) { p ; p p width; p p ; p p height; p p ; p p x; p p ; p p y; retval window.(url, windowName, p); } { win window.( url, , y x scrollbars width height ); (); win.(); }}
将键值对拼接成URL带参数
const obj { aUrl [] (key, value) { key value } ( k obj) { aUrl.((k, obj[k])) } (aUrl.()) }
去掉url前缀
(a) { a a .(, ) .(, ) .(, ); ( (a) .() .() ) { a (a.(, )); } a;}
替换全部
.. (s1, s2) { this.( (s1, ), s2);};
resize的操作
(() { () { w document. ? document.. : document.., r , b .(document.), classname b.; (w r) { } { } }; (window.) { window.(, () { (); }); } (window.) { window.(, () { (); }); } ();})();
滚动到顶部
() { c document.. document..; (c ) { window.(scrollToTop); window.(, c c / ); }}
设置cookie值
(name, value, ) { d (); offset ; utc d.() d.() * ; nd utc * offset; exp (nd); exp.(exp.() * * * ); document. name (value) exp.() ;}
设为首页
() { (document.) { document... ; document..(); } (window.) { (window.) { { netscape...( ); } (e) { ( ); } } prefs .[ ].(..); prefs.(, ); }}
按字母排序,对每行进行数组排序
() { text . .() .() .(); test . .() .() .() .(); . . != text ? text : test;}
延时执行
time { (resolve (resolve, time));};().(() { console.();});* sleepGenerator(time) { ((resolve, reject) { (resolve, time); });}() .() ..(() { console.(); }); (time) { (resolve (resolve, time));} () { out (); console.(); out;}(); (callback, time) { ( callback ) { (callback, time); }} () { console.();}(output, );
判断是否以某个字符串开头
.. (s) { this.(s) ;};
清除脚本内容
(s) { s.(, );}
时间个性化输出功能
(time) { date (time), curDate (), year date.(), month date.() , day date.(), hour date.(), minute date.(), curYear curDate.(), curHour curDate.(), timeStr; (year curYear) { timeStr year month day hour minute; } { pastTime curDate date, pastH pastTime / ; (pastH curHour) { timeStr month day hour minute; } (pastH >= ) { timeStr hour minute ; } { pastM curDate.() minute; (pastM ) { timeStr pastM ; } { timeStr ; } } } timeStr;}
全角转换为半角函数
(str) { result ; ( i ; i str.; i) { code str.(i); (code >= code <= ) { result .(str.(i) ); } (code ) { result .(str.(i) ); } { result str.(i); } } result;}
半角转换为全角函数
(str) { result ; ( i ; i str.; i) { code str.(i); (code >= code <= ) { result .(str.(i) ); } (code ) { result .(str.(i) ); } { result str.(i); } } result;}
金额大写转换函数
(tranvalue) { { i ; dw2 (, , ); dw1 (, , ); dw ( , , , , , , , , , ); source (tranvalue); num source[]; dig source[]; k1 ; k2 ; sum ; str ; len source[].; (i ; i <= len; i) { n source[].(len i); bn ; (len i >= ) { bn source[].(len i ); } sum sum (n); (sum != ) { str dw[(n)].(str); (n ) sum ; } (len i >= ) { (k1 != ) { (bn != ) { str dw1[k1].(str); } k1; } { k1 ; temp str.(); (temp temp ) str str.(, str. ); str dw2[k2].(str); sum ; } } (k1 ) { k2; } } strdig ; (dig != ) { n dig.(); (n != ) { strdig dw[(n)] ; } n dig.(); (n != ) { strdig dw[(n)] ; } } str strdig; } (e) { ; } str;} (tranvalue) { value (, ); temp tranvalue.(); ( i ; i temp.; i) { value temp; } value;}
清除空格
.. () { reExtraSpace ; this.(reExtraSpace, );}; (s) { s.(, );} (s) { s.(, );}
随机数时间戳
() { a ., b parseInt; ( ( ()).() b( * a()) b( * a()) b( * a()) );}
实现utf8解码
(str_data) { tmp_arr [], i , ac , c1 , c2 , c3 ; str_data ; (i str_data.) { c1 str_data.(i); (c1 ) { tmp_arr[ac] .(c1); i; } (c1 c1 ) { c2 str_data.(i ); tmp_arr[ac] .(((c1 & ) ) | (c2 & )); i ; } { c2 str_data.(i ); c3 str_data.(i ); tmp_arr[ac] .( ((c1 & ) ) | ((c2 & ) ) | (c3 & ) ); i ; } } tmp_arr.();}
赞 (0)