事件
JavaScript有三种事件模型:内联模型、脚本模型和DOM2模型。
内联模型,onclick是事件处理函数
<input type="button" value="按钮" onclick="alert('Lee');">
<input type="button" value="按钮" onclick="box();">
function box(){ //如果放在一个匿名函数里面,就看不到了
alert('Lee');
}
脚本模型
<input type="button" value="按钮">
window.onload=function(){
var input=document.getElementsByTagName("input")[0];
//对象.事件处理函数=函数名或者匿名函数
input.onclick=function(){
alert("Lee");
}
}
或者:
window.onload=function(){
var input=document.getElementsByTagName("input")[0];
input.onclick=box;
}
function box(){
alert('Lee');
}
--------
window.onload=function(){
var input=document.getElementsByTagName("input")[0];
input.onclick=box;
function box(){
alert('Lee');
}
}
PS:可以放到外面,也可以和input同级