1.text输入框的value值不是数字替换为空
html代码
<input type="text" style="width:150px" id="ts_math_input" placeholder="请输入题:" />
js代码
ts_math_input.oninput = function () {
console.log(9);
event.target.value=event.target.value.replace(/[^0-9]/g,'')
}
2.file单文件,部分类型文件简单读写
html代码
<input type="file" onchange="change(event)">
<img src="" id="image" alt="">
js代码
function change(e) {
let readerfile = e.currentTarget.files[0];
console.log(readerfile);
const reader = new FileReader();
//对于json类型文件,有需要转成json.parse格式的文件
if (readerfile.type == "application/json") {
reader.onload = function () {
var result = JSON.parse(reader.result)
console.log(result);
}
reader.readAsText(readerfile);
}else if(readerfile.type == "image/png"){
//png图片
reader.onload = function () {
image.src = reader.result;
}
reader.readAsDataURL(readerfile);
} else {
reader.onload = function () {
console.log(reader.result);
}
reader.readAsText(readerfile);
}
}
2.file单文件判断文件大小
html代码
<input type="file" onchange="fileSize(event)">
js代码
function fileSize(){
let readerfile = e.currentTarget.files[0];
console.log(readerfile);
if (readerfile.size == 0) {
return '0B'
} else if (!readerfile.size) {
return '-'
}
let fileSizeByte = readerfile.size;
let fileSizeMsg = "";
if (fileSizeByte > 0 && fileSizeByte < 1024) {
fileSizeMsg = fileSizeByte + 'B';
} else if (fileSizeByte == 1024) {
fileSizeMsg = "1KB";
} else if (fileSizeByte > 1024 && fileSizeByte < 1048576) {
fileSizeMsg = (fileSizeByte / 1024).toFixed(2) + "KB";
} else if (fileSizeByte == 1048576) {
fileSizeMsg = "1MB";
} else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824) {
fileSizeMsg = (fileSizeByte / (1024 * 1024)).toFixed(2) + "MB";
} else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824) {
fileSizeMsg = "1GB";
} else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776) {
fileSizeMsg = (fileSizeByte / (1024 * 1024 * 1024)).toFixed(2) + "GB";
} else {
fileSizeMsg = "文件超过1TB";
}
console.log(fileSizeMsg);
}