Fork me on GitHub

js常用工具函数汇总

下面给大家梳理了我平时在工作中常用的js方法,本来是打算作为自己的工具库的,为了发现代码的最佳方案,再此贴出,望相互交流。

程序员必读

Css3编码技巧

1.检测平台(设备)类型

1
2
3
4
5
var isWechat = /micromessenger/i.test(navigator.userAgent),
isWeibo = /weibo/i.test(navigator.userAgent),
isQQ = /qq\//i.test(navigator.userAgent),
isIOS = /(iphone|ipod|ipad|ios)/i.test(navigator.userAgent),
isAndroid = /android/i.test(navigator.userAgent);

2.时间格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
// 时间格式化
function format_date(timeStamp) {
var date = new Date(timeStamp);
return date.getFullYear() + "年"
+ prefix_zero(date.getMonth() + 1) + "月"
+ prefix_zero(date.getDate()) + "日 "
+ prefix_zero(date.getHours()) + ":"
+ prefix_zero(date.getMinutes());
}
// 数字格式化
function prefix_zero(num) {
return num >= 10 ? num : "0" + num;
}

3.倒计时函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 倒计时时间格式化
function format_time(timeStamp) {
var day = Math.floor(timeStamp / (24 * 3600 * 1000));
var leave1 = timeStamp % (24 * 3600 * 1000);
var hours = Math.floor(leave1 / (3600 * 1000));
var leave2 = leave1 % (3600 * 1000);
var minutes = Math.floor(leave2 / (60 * 1000));
var leave3 = leave2 % (60 * 1000);
var seconds = Math.floor(leave3 / 1000);
if (day) return day + "天" + hours + "小时" + minutes + "分";
if (hours) return hours + "小时" + minutes + "分" + seconds + "秒";
if (minutes) return minutes + "分" + seconds + "秒";
if (seconds) return seconds + "秒";
return "时间到!";
}

4.判断设备是否支持触摸事件

1
var isSupportTouch = ("ontouchstart" in document.documentElement) ? true : false;

5.js+rem实现移动端适配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
var fontSize = 20;
docEl.style.fontSize = fontSize + 'px';
var docStyles = getComputedStyle(docEl);
var realFontSize = parseFloat(docStyles.fontSize);
var scale = realFontSize / fontSize;
console.log("realFontSize: " + realFontSize + ", scale: " + scale);
fontSize = clientWidth / 667 * 20;
if(isIphoneX()) fontSize = 19;
fontSize = fontSize / scale;
docEl.style.fontSize = fontSize + 'px';
};
// Abort if browser does not support addEventListener
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);

// iphoneX判断
function isIphoneX(){
return /iphone/gi.test(navigator.userAgent) && (screen.height == 812 && screen.width == 375)
}

})(document, window);

6.检查是否为中文

1
let isCN = (str) => { return /^[\u4e00-\u9fa5]*$/.test(str) }

7.禁用enter表单自动提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//禁用Enter键表单自动提交
document.onkeydown = function(event) {
var target, code, tag;
if (!event) {
event = window.event; //针对ie浏览器
target = event.srcElement;
code = event.keyCode;
if (code == 13) {
tag = target.tagName;
if (tag == "TEXTAREA") { return true; }
else { return false; }
}
}
else {
target = event.target; //针对遵循w3c标准的浏览器,如Firefox
code = event.keyCode;
if (code == 13) {
tag = target.tagName;
if (tag == "INPUT") { return false; }
else { return true; }
}
}
};

8.敏感符号转义

1
2
3
4
5
6
7
8
9
10
11
function entities(s) {
let e = {
'"': '"',
'&': '&',
'<': '&lt;',
'>': '&gt;'
}
return s.replace(/["<>&]/g, m => {
return e[m]
})
}
Mr XuJiang wechat
欢迎您扫一扫上面的微信二维码,小猪送你啦!
0%