常用js代码段

2017-12-27  本文已影响0人  一蓑烟雨任平生_cui

1. 解析查询字符串

  1. 方法一:
const getQuerySearch = () => {
    // 获取 ?后面的内容
    let search = location.search.length ? location.search.substr(1)  :  '';
    const obj = {};
    if (search.length) {
        const arr = search.split('&');
        arr.forEach(item => {
            const [key, value] = item.split('=');
            obj[key] = value;
            // 或
            // [key, obj[key]] = item.split('=')
        })
    }
    return obj;
}
  1. 方法二:
const { search } = location;
const str = search.length ? search.substr(1) : '';
const rest = Object.fromEntries(new URLSearchParams(str).entries())

2. 解决安卓手机 键盘出来覆盖页面(页面没有上移)

if (/Android/gi.test(navigator.userAgent)) {
    window.addEventListener('resize', function () {
        if (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA') {
            window.setTimeout(function () {
                document.activeElement.scrollIntoViewIfNeeded();
            }, 0)
        }
    })
}
上一篇 下一篇

猜你喜欢

热点阅读