JavScript------day1

1、什么是JavaScript
JavaScript是SUN与网景发明的,基于对象的,区分大小写的,弱的脚本语言。
2、JavaScript能做什么
表单验证、客户端编程以及特效等。
3、js写在哪
标签内、js块内、外部导入
例如

<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript">
	function test1() {
		alert("写在js块内");	
	}
</script>
<script src="js--day1.js"></script>
</head>

<body>
<input type="button" value="button1" onClick="alert('写在标签事件内')"/>
<input type="button" value="button2" onClick="test1()"/>
<input type="button" value="button3" onClick="test2()"/>
</body>
</html>
// JavaScript Document
function test2() {
	alert("外部导入");	
}

4、接下来我们做一个简单的计算器,在例子中讲解js如何获取到form表单中的值

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript">
function check(num1, num2, temp) {
	if(num1 == "") {
		alert("num1不能为空");
		return false;
	}
	if(num2 == "") {
		alert("num2不能为空");
		return false;
	}
	if(isNaN(num1)) {
		alert("num1必须为数字");	
		return false;
	}
	if(isNaN(num2)) {
		alert("num2必须为数字");	
		return false;
	}
	if(temp == '/') {
		if(num2 == '0') {
			alert("除数不能为0");
			return false;
		}
	}
	return true;
}
function getResult(temp) {
	var num1 = document.getElementById("num1").value;//我们用document.getElementById("表单中元素的id名").value来获取表单中元素的值
	var num2 = document.getElementById("num2").value;		
	if(check(num1, num2, temp)) {
		if(temp == '+') {
			document.getElementById("result").value = parseInt(num1) + parseInt(num2);		
		}
		if(temp == '-') {
			document.getElementById("result").value = parseInt(num1) - parseInt(num2);		
		}
		if(temp == '*') {
			document.getElementById("result").value = parseInt(num1) * parseInt(num2);		
		}
		if(temp == '/') {
			document.getElementById("result").value = parseInt(num1) / parseInt(num2);		
		}
	}	
}

</script>
</head>

<body>
<h2 align="center">计算器</h2>
<p align="center">
    num1:<input type="text" id="num1" name="num1"/><br/>
    num2:<input type="text" id="num2" name="num2"/><br/>
    <input type="button" value="+" onClick="getResult('+')"/>
    <input type="button" value="-" onClick="getResult('-')"/>
    <input type="button" value="*"onClick="getResult('*')"/>
    <input type="button" value="/" onClick="getResult('/')"/><br/>
    result:<input type="text" id="result" name="result"/> 
</p>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值