今天学习了javascirpt的一些基础知识,有关事件绑定的一些方法:
1.<input type="button" onclick="clickHandler();" value="click"/>
这是最简单也是最直接的一种方法,不过一般程序中很少用。
2. <input type="button" id="button" value="click"/>
<script type="text/javascript">
var v=document.getElementById("button");
v.onclick=clickHandler;//后面不要带括号;
function clickHandler(event){ //为了使firefox支持event一定要存在,为了兼容ie里面参数只能是event;
alert(event.type);
}
</script>
3.. <input type="button" id="button" value="click"/>
<script type="text/javascript">
var v=document.getElementById("button");
v.onclick=function clickHandler(event){ //为了使firefox支持event一定要存在,为了兼容ie里面参数只能是event;
alert(event.type);
function (event){ //为了使firefox支持event一定要存在,为了兼容ie里面参数只能是event;
alert(event.type);
}
//直接省略clickHandler
</script>
总结,由于本人的对知识的掌握有限,只能分享这些,有什么错误,希望大家可以提出,欢迎大家的指点;
本文介绍了三种JavaScript事件绑定的方法:内联绑定、通过JavaScript设置以及使用匿名函数。每种方法都有其适用场景,文章提供了详细的示例代码,并强调了跨浏览器兼容性的重要性。
绑定事件的几种方法&spm=1001.2101.3001.5002&articleId=82403743&d=1&t=3&u=e24cbea0ee454be28ac85b6778ff4100)
1969

被折叠的 条评论
为什么被折叠?



