1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <script type="text/javascript"> 7 window.onload = function(){ 8 var oInput01 = document.getElementById('input01'); 9 var oInput02 = document.getElementById('input02'); 10 var oBtn = document.getElementById('btn'); 11 oBtn.onclick = function(){ 12 var iVal01 = parseInt(oInput01.value); // 将输入的字符串转换成int类型 13 var iVal02 = parseInt(oInput02.value); 14 alert(iVal01 + iVal02); 15 } 16 } 17 </script> 18 </head> 19 <body> 20 <input type="text" name="" id="input01">+ 21 <input type="text" name="" id="input02"> 22 <input type="button" name="" id="btn" value="相加"> 23 </body> 24 </html>