JavaScript
- 弱语言 不需要声明可以直接使用
- 脚本语言
- 基于对象
- 区分大小写
- Sun与网景公司开发
能做什么?
- 表单验证 Form
实例
(1)
<script> type = "text/javascript"
function test(){
alert("弹出的内容!!")
</script> //定义test的方法
<input type = "button" value = "写再js内" onclick = "test()" //点击事件添加test方法
(2)
//除了上述方法
//就是在外部声明javascript代码,然后在内部直接调用!!!
<script src = "haha.js"></script>
方法声明
与java声明的区别:
int add ( int a , int b) {
return a+ b;
}
****************************
/***
声明变量前没有数据类型!
调试工具: F12 add中的a,b 加上var 修饰时,不能使用会出现add 方法不能被定义
类型是根据你所赋值的类型所决定!!!
***/
function add (a , b) {
return a+ b ;
}
获取表单的值
- document.form.标签名
function getValue (){
var ageText= document.forms['testForm']['age'] ;// 通过获取forms确定获取form
//然后在获取定义的testforms 在获取到age 节点 得到age的值
var agevalue = ageText.value;
- document.getElementById
/***
另一种表达方式
***/
var ageText = docunment.getElementById("age"); //直接获取到age节点
}
解释执行
读一行 , 走一行!
parseint 可以直接调用 是内置函数
isNaN
判断字符串是否为数字
不是数字就返回true
是数字返回false
简单的计算器
num1 : <input type = "text" id = "num1" name = "num1"/>
num2 : <input type = "text" id = "num2" name = "num2"/>
<input type = "button" value = "+" onclick =
<input type = "button" value = "-"
<input type = "button" value = "*"
<input type = "button" value = "/"<br>
result: <input type = "text" id= "result" name = "result"
function getResult(){
var num1 = document.getElement("num1").value ;
var num2 = document.getElement("num2").value ;
if(num1 == null){
alert("必须输入字符")
return;
} // 对输入数值进行验证
if(isNaN){
alert("必须输入数字")
return;
// 没有return就会继续执行下面的代码 判断失去了意义
//判断就是要让不能运行的部分回滚 结束继续执行
document.getElementById("result").value= num1 + num2;//获取到的值都是字符串
// 所以会导致字符串相加 1+2 = 12 这时候需要对获取到的值进行转化
value= paseint(num1) + paseint(num2);
//在调用+-*/时 用一个type 来判断不同的代码块
if(type == "/"){//判断除数不能为0
if(num2 == 0){
alert("除数不能为零")
return;
resultNode.value = paseint(num1) / paseInt(num2)
}
}