<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <script> var a='12'; alert(parseInt(a)+1);//字符串转数字 </script> </body> </html>
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <input type="text" id="txt1" /> <br/> <br/> <input type="text" id="txt2" /> <br/> <br/> <input type="button" id="btn" value="点击" /> <script> var otxt1=document.getElementById('txt1'); var otxt2=document.getElementById('txt2'); var obtn=document.getElementById('btn'); obtn.onclick=function() { var n1=parseInt(otxt1.value); var n2=parseInt(otxt2.value); alert(n1+n2) } </script> </body> </html>
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <input type="text" id="txt1" /> <br/> <br/> <input type="text" id="txt2" /> <br/> <br/> <input type="button" id="btn" value="点击" /> <script> var otxt1=document.getElementById('txt1'); var otxt2=document.getElementById('txt2'); var obtn=document.getElementById('btn'); obtn.onclick=function() { var n1=parseInt(otxt1.value); var n2=parseInt(otxt2.value); if(isNaN(n1)) { alert('你输入的第一个数字有误!'); } else if(isNaN(n2)) { alert('你输入的第二个数字有误!'); } else { alert(n1+n2); } } </script> </body> </html>