(012)显式类型转换

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title>显式类型转换</title>
 6     <script type="text/javascript">
 7 
 8         window.onload = function() {
 9 
10             var txt1Node = document.getElementById("txt1");
11             var txt2Node = document.getElementById("txt2");
12             var btn1 = document.getElementById("btn1");
13 
14             btn1.onclick = function() {
15                 
16                 //parseInt显式类型转换
17                 var num = parseInt(txt1Node.value) + parseInt(txt2Node.value);
18                 if(isNaN(num)) {
19                     alert("非法数字,请重新输入");
20                 }
21                 else {
22                     alert(num);
23                 }
24             };
25         };
26         
27     </script>
28 </head>
29 
30 <body>
31     <input id="txt1" type="text"/>
32     <input id="txt2" type="text"/>
33     <input id="btn1" type="button" value="求和"/>
34 </body>
35 </html>

 

posted @ 2014-01-21 23:34  雪中飞雁  阅读(72)  评论(0)    收藏  举报