获取查询字符串参数
// Assuming "?post=1234&action=edit"var urlParams = new URLSearchParams(window.location.search);console.log(urlParams.has('post')); // trueconsole.log(urlParams.get('action')); // "edit"console.log(urlParams.getAll('action')); // ["edit"]console.log(urlParams.toString()); // "?post=1234&action=edit"console.log(urlParams.append('active', '1')); // "?post=1234&action=edit&active=1"
创建空对象
我们可以使用对象字面量{}来创建空对象,但这样创建的对象有隐式原型proto和一些对象方法比如常见的hasOwnProperty,下面这个方法可以创建一个纯对象。
let dict = Object.create(null);// dict.__proto__ === "undefined"// No object properties exist until you add them
数组清洗
洗掉数组中一些无用的值,如0, undefined, null, false等
myArray.map(item => {// ...})// Get rid of bad values.filter(Boolean);
键盘弹出挡表单
window.addEventListener('resize', function () {if (document.activeElement.tagName === 'INPUT' ||document.activeElement.tagName === 'TEXTAREA' ||document.activeElement.getAttribute('contenteditable') == 'true') {window.setTimeout(function () {if ('scrollIntoView' in document.activeElement) {document.activeElement.scrollIntoView();} else {// @ts-ignoredocument.activeElement.scrollIntoViewIfNeeded();}}, 0);}})
解决 iOS iPhone 下键盘收起[页面不收起]导致点击事件失效问题
1 scrollIntoView方案
在 IOS 11 上某个版本的时候,用 scrollIntoView会遇到页面卡住滑不动的情况,并且只在IOS11上的某个小版本出现
;(/iphone|ipod|ipad/i.test(navigator.appVersion)) && document.addEventListener('blur', (e) => {['input', 'textarea'].includes(e.target.localName) && document.body.scrollIntoView(false) }, true)
2 document.body方案
// 自行实现是否是 iOS 的判断,一般把 ipad,ipod, iphone 跟 navigagor.userAgent indexOf 一下就行if (this.userAgent.iOS) {document.addEventListener('blur', e => {['input', 'textarea'].indexOf(e.target.localName) !== -1 && (document.body.scrollTop = document.body.scrollTop)}, true)}
