java学习

java学习

 

javascript事件对象event

在非ie中
<html>
 <head></head>
 <body>
  <div id="div1">1111111111
   <div id="div2">2222222222</div>
  </div>
  <input type="button"  id="btn"  />
  <a href="http://www.baidu.com" id="mya">163</a>
  <a href="javascript:;" >普通按钮,不跳转</a>
  <script type="text/javascript">
    document.getElementById('btn').onclick = function(event){
    //在非ie中
    alert(event.type);
    alert(event.target);
    }
    document.getElementById('mya').onclick = function(event){
    alert(event.cancelable);
    if(event.cancelable){
     event.preventDefault();//阻止事件的发生
    }
    }
    document.getElementById('btn').onclick = function(event){
    //alert(1);
    event.stopPropagation();//阻止事件的传播
    
    }
    document.body.onclick=function(){
    //alert(2);
    }
  </script>
 </body>
</html>
在ie中
<html>
 <head></head>
 <body>
  <div id="div1">1111111111
   <div id="div2">2222222222</div>
  </div>
  <input type="button"  id="btn"  />
  <a href="http://www.baidu.com" id="mya">163</a>
  <a href="javascript:;" >普通按钮,不跳转</a>
  <script type="text/javascript">
  var event;
  function getEvent(){
   event=window.event;
  }
    document.getElementById('btn').onclick = function(event){
   getEvent();
   alert(event.type);
   alert(event.srcElement);//得到目标元素
   event.cancelBubble = true;//取消事件冒泡
   event.returnValue = false;//取消默认行为
    }
   //使用下面的方法可以在参数中得到event
   document.getElementById('btn').attachEvent('onclick',function(event){
    
   });
  </script>
 </body>
</html>

posted on 2013-06-27 13:56 杨军威 阅读(236) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜