<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<script>
function convert (ID) {
var px_val = document.getElementById (' Pxtoem '). Value,
Em_val = document.getElementById (' emtopx '). Value,
Base_val = document.getElementById (' basepx '). Value;
if (!base_val) {
document.getElementById (' Tip '). Value = ' Basic unit cannot be empty ';
Return
}else{
document.getElementById (' Tip '). Value = ';
}
Base_val = Base_val.replace (/[^0-9.) /g, ");
if (id = = = ' Pxtoemconvert ') {
if (!px_val) {
document.getElementById (' Tip '). Value = ' PX to em cannot be null ';
Return
}
Px_val = Px_val.replace (/[^0-9.) /g, ");
document.getElementById (' Tip '). Value = ';
document.getElementById (' result '). Value= ((px_val/base_val). toFixed (3) + ' em ');
}else if (id = = = ' Emtopxconvert ') {
if (!em_val) {
document.getElementById (' Tip '). Value = ' EM to px cannot be empty ';
return;
}
Em_val = Em_val.replace (/[^0-9.) /g, ");
document.getElementById (' Tip '). Value = ';
document.getElementById (' result '). Value= (parseint (Em_val * base_val) + ' px ');
}
}
</script>
<body>
<br>
<label for= "basepx" > Basic unit:</label>
<input type= "number" id= "basepx" >px
<br>
<label for= "Pxtoem" >px to Em:</label>
<input type= "number" id= "Pxtoem" >px
<br>
<label for= "Emtopx" >em to Px:</label>
<input type= "number" id= "EMTOPX" >em
<br>
<label for= "Result" > conversion result:</label>
<input type= "text" id= "result" >
<br>
<input type= "button" id= "Pxtoemconvert" onclick= "Convert (this.id)" value= "PX to EM Conversion" >
<input type= "button" id= "Emtopxconvert" onclick= "Convert (this.id)" value= "EM to px Conversion" >
<br>
<label for= "Tip" > Tips:</label>
<input type= "text" id= "Tip" readonly>
</body>
Related articles Link:
Http://www.cnblogs.com/leejersey/p/3662612.html
EM and px convert each other