1.特殊字符转义
const escape = (str) => str.replace(/[&<>"']/g, (m) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[m]))
2.字符串转驼峰
const toCamelCase = (str) => str.trim().replace(/[-_\s]+(.)?/g, (_, c) => (c && c.toUpperCase()));
3.滚动到顶部
$('button').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);});
4.滚动指定位置
$('button').click(function({$('html,body').animate({scrollTop:$('positionSite').offset().top}
5.滚动到底部
$('button').click(function({$('html,body').animate({scrollTop:$('.bottom').offset().top}, 800);})
6.隐藏元素
const hideElement = (el, removeFromFlow = false) => {removeFromFlow ? (el.style.display = 'none'):(el.style.visibility = 'hidden')} //true删除元素 false隐藏元素
7.复制到剪切板
const copyToClipboard = (text) => navigator.clipboard && navigator.clipboard.writeText && navigator.clipboard.writeText(text)
8.铺平数组
const flat = (arr) => arr.reduce((a, b) => (Array.isArray(b) ? [...a, ...flat(b)] : [...a, b]), [])
9.点击页面任意位置自动全屏
window.onclick = function (e) {
if (e.isTrusted && e.currentTarget == window && e.pointerId >= 0) {
document.documentElement.requestFullscreen()
}
}
10.屏幕按键
document.onkeydown = function (event) {
if (event.ctrlKey && event.shiftKey && event.keyCode == 73) {
return false;
}
if (event.ctrlKey && window.event.keyCode == 65) { //禁用ctrl + a 功能
return false;
}
if (event.ctrlKey && window.event.keyCode == 67) { //禁用ctrl + c 功能
return false;
}
if (event.ctrlKey && window.event.keyCode == 83) { //禁用ctrl + s 功能
return false;
}
if (event.ctrlKey && window.event.keyCode == 86) { //禁用ctrl + v 功能
return false;
}
if ((event.altKey) && ((window.event.keyCode == 37) || (window.event.keyCode == 39))) {
return false;
} // 不准你使用ALT+方向键前进或后退网页
if (event.ctrlKey && event.keyCode == 82) {
return false;
} //Ctrl + R
if ((event.ctrlKey) && (event.keyCode == 78)) {
return false; //屏蔽 Ctrl+n
}
if ((event.shiftKey) && (event.keyCode == 121)) { //屏蔽 shift+F10
return false;
}
if (window.event.srcElement.tagName == "A" && window.event.shiftKey) {
return false; //屏蔽 shift 加鼠标左键新开一网页
}
if ((window.event.altKey) && (window.event.keyCode == 115)) { //屏蔽Alt+F4
return false;
}
if (window.event.keyCode == 116) { //禁用F5
return false;
}
if (window.event.keyCode == 123) { //禁用F12
return false;
}
if (window.event.keyCode == 27) { //禁用ESC
return false;
}
if (window.event.keyCode == 116) { //禁用F5
return false;
}
}