<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> Upload Images </title>
<style>
body,div,img{
margin:0 Auto;
padding:0;
}
html{
width:100%;
height:100%;
}
body{
line-height:20px;
Font-family: "Microsoft Jas Black";
width:1000px;
height:100%;
Background-color: #000;
}
. upload-img{
position:relative;
Text-align:center;
line-height:300px;
Float:left;
margin-top:100px;
Background-color: #fff;
Color: #333;
font-size:30px;
width:300px;
height:300px;
margin-left:15px;
margin-right:15px;
}
. upload-img. imgs{
Position:absolute;
left:0;
top:0;
width:100%;
height:100%;
}
. upload-img:hover{
Color: #f90;
}
. upload-img. btn{
width:100%;
height:100%;
opacity:0;
Filter:alpha (opacity=0);
Z-index:2;
Position:absolute;
left:0;
top:0;
Cursor:pointer;
}
</style>
<body>
<div class= "Upload-img" >
Upload image
<input type= "File" class= "btn" onchange= "uploadimg (this,1);"/>
</div>
<div class= "Upload-img" >
Upload image
<input type= "File" class= "btn" onchange= "uploadimg (this,2);"/>
</div>
<div class= "Upload-img" >
Upload image
<input type= "File" class= "btn" onchange= "uploadimg (this,3);"/>
</div>
<script>
Upload image
function Uploadimg (that,num) {
var Parentnode=that.parentnode;
var images=parentnode.getelementsbytagname ("img");
if (images.length==0) {
Createimg (Parentnode,num);
}
var Img=document.getelementbyid ("img" +num);
var f=that.files;
if (F&&f[0]) {
Checkedimg (THAT,F,IMG);
}else{
Compatible with IE
var Srcval=that.value;
if (!srcval) {
Alert ("You performed a cancel operation");
Img.setattribute ("src", "");
Srcval= "";
}else{
var houzui=srcval.substr (Srcval.lastindexof ("."))
. replace (".", "");
if (houzui== "jpg" | | houzui== "JPEG") {
Img.setattribute ("src", srcval);
}else{
Alert ("Please upload a picture with suffix jpg,jpeg format");
Img.setattribute ("src", "");
Srcval= "";
}
}
}
}
Create a picture
function Createimg (parentnode,num) {
var imgelement=document.createelement ("img");
Imgelement.classname= "IMGs";
Imgelement.id= "img" +num;
Parentnode.appendchild (imgelement);
}
Determine the type of picture, size
function Checkedimg (that,f,img) {
Type of picture
if (f[0].type== "Image/jpeg") {
Image size cannot exceed 3M
if (f[0].size/1024/1024<3) {
var Imgsrc=window. Url.createobjecturl (That.files[0]);
Img.setattribute ("src", imgsrc);
}else{
Alert ("Upload image size cannot exceed 3M");
Empty file fields
F=[];
Img.setattribute ("src", "");
return false;
}
}else{
Alert ("Please upload a picture with suffix jpg,jpeg format");
Empty file fields
F=[];
Img.setattribute ("src", "");
return false;
}
}
</script>
</body>
Native JS for instant preview code