js中事件的使用方式如onclick();

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>亲测测试</title>
<script type="text/javascript">
function oclick(){
alert('你点击了我');
}
</script>
</head>

<body>
<!--//方式一-->
<input onclick="alert('你点击了我')" value="点我1" type="button">

<input onclick="oclick()" value="点我2" type="button">


<!--// 方式二-->

<input id="demo" type="button" value="点我3">
<script>
document.getElementById("demo").onclick=function(){
alert(this.getAttribute("type"));
}
</script>


<!--//方式三 绑定事件监听函数
addEventListener()函数 /attachEvent()来绑定监听事件;-->
<!--elementObject.addEventListener(eventName,handle,useCapture);-->
<script type="text/javascript">
function addEvent(obj,type,handle){
try{ // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
obj.addEventListener(type,handle,false);
}catch(e){
try{ // IE8.0及其以下版本
obj.attachEvent('on' + type,handle);
}catch(e){ // 早期浏览器
obj['on' + type] = handle;
}
}
}
addEvent(document.getElementById("demo"),"click",myAlert);
function myAlert(){
alert('你点击了我');
}
</script>

</body>
</html>

 

posted on 2017-12-18 19:56  AJin0802  阅读(14131)  评论(0编辑  收藏  举报

导航