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;
}
- 方法二:
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)
}
})
}
网友评论