廢話不多說,直接上代碼
html
<input type="file" name="" value="上傳文件" id='file'>
js
file.onchange = function(){
// 判斷圖片
if (!/image\/\w+/.test(this.files[0].type)) {
alert('上傳正確圖片');
return false;
}
// 判斷視頻
if (!/image\/\w+/.test(this.files[0].type)) {
alert('上傳正確視頻');
return false;
}