JS一些记录

限制输入框输入数字:oninput = "value=value.replace(/[^\d]/g,'')"        /g全局匹配 ^非

       数字加小数点      value=value.replace(/[^\d^.]/g,'')

 

复制到剪切板 text为要复制的内容

f

function copyText(text){
// 数字没有 .length 不能执行selectText 需要转化成字符串
const textString = text.toString();
let input = document.querySelector('#copy-input');
if (!input) {
input = document.createElement('input');
input.id = "copy-input";
input.readOnly = "readOnly"; // 防止ios聚焦触发键盘事件
input.style.position = "absolute";
input.style.left = "-1000px";
input.style.zIndex = "-1000";
document.body.appendChild(input)
}

input.value = textString;
// ios必须先选中文字且不支持 input.select();
selectText(input, 0, textString.length);
console.log(document.execCommand('copy'), 'execCommand');
if (document.execCommand('copy')) {
document.execCommand('copy');

alert('已复制到粘贴板');

}
input.blur();

// input自带的select()方法在苹果端无法进行选择,所以需要自己去写一个类似的方法
// 选择文本。createTextRange(setSelectionRange)是input方法
function selectText(textbox, startIndex, stopIndex) {
if (textbox.createTextRange) {//ie
const range = textbox.createTextRange();
range.collapse(true);
range.moveStart('character', startIndex);//起始光标
range.moveEnd('character', stopIndex - startIndex);//结束光标
range.select();//不兼容苹果
} else {//firefox/chrome
textbox.setSelectionRange(startIndex, stopIndex);
textbox.focus();
}
}
};

JSON.stringify();转化成字符串

JSON.parse();转化成json

 

Math.ceil() 返回大于等于数字参数的最小整数(取整函数),对数字进行上舍入(只要整数,抹去小数+1)

Math.floor() 返回小于等于数字参数的最大整数,对数字进行下舍入(只要整数,抹去小数)

Math.round() 返回数字最接近的整数,四舍五入

 

posted @ 2019-06-14 17:12  龙7龙  阅读(396)  评论(0编辑  收藏  举报