功能:在文本框中输入任意数字点按钮都能求和,输错点击会提醒错误
事件:onclick
属性:input中的value
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="css1.css" rel="stylesheet" type="text/css"
charset="UTF-8">
</head>
<body>
<input id="txt1" type="text"/ >
<input id="txt2" type="text"/ >
<input id="btn" type="button" value="数字求和"/ >
<script src="js1.js"> </script>
</body>
</html>
////////////////////js
window.onload=function(){
var oBtn=document.getElementById("btn");
oBtn.onclick=function(){
var oTxt1=document.getElementById("txt1");
var oTxt2=document.getElementById("txt2");
var n1=oTxt1.value;
var n2=oTxt2.value;
if(isNaN(n1)==true){
alert("您输入的第一个数字有误");
}
else if(isNaN(n2)==true){
alert("您输入的第二个数字有误");
}
else{
alert(parseInt(n1)+parseInt(n2));
}
};
};