29-事件对象

  1. 事件对象指的是event对象

$(document).ready(function(){
$('input').on('click',function(e){
  alert('点击时候是否同时按下ctrl键'+e.ctrlKey);
});
  $("input").on('click',123,function(e){
   alert('事件类型字符串'+e.type);
   alert('获取绑定事件的dom元素(点了哪个就是哪个)'+e.target);
   alert('获取监听元素的dom,绑定哪个就是哪个'+e.currentTarget);
   alert('临近的dom元素(mouseover调用)'+e.relatedTarget);
   alert('获取额外的数据'+e.data);
   alert('获取时间戳'+e.timeStamp);
   alert('获取鼠标的左中右键123,键盘按键'+e.which);
});
   $(document).on('click',function(e){
   alert('pageX'+e.pageX+'screenX'+e.screenX);
});

//获取上一个相同事件的返回值e.result
   $('input').on('click',function(e){
    return 123;
});
  $('input').on('click',function(e){
    alert(e.result);
});
});

2.冒泡行为

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>

$(document).ready(function(){
  $('input').on('click',function(e){
    alert('input');
});
$('div').on('click',function(e){
    alert('div');
});
$(document).on('click',function(e){
    alert('document');
});
});
</script>
</head>
<body>
<div style="background:red;display:block;height:200px;width:200px;">
<input type="button" value="按钮"/>
</div>
</body>
</html>

blob.png

效果,点击document,弹出document,点击div,弹出div,document,点击input,弹出,input,div,document

阻止冒泡行为:

e.stopPropagation();

3.默认行为

  1. 超链接跳转2.表单提交

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>

$(document).ready(function(){
  //阻止超链接跳转
  $('a').on('click',function(e){
    e.preventDefault();
    alert('a');
  });

  //阻止表单提交1
  $('input').on('click',function(e){
    e.preventDefault();
   });
 //阻止表单提交2
  $('form').submit(function(e){
    e.preventDefault();
  });
});
</script>
</head>
<body>
<a href="http://www.baidu.com">百度</a>
<form action="http://www.baidu.com">
  <input type="submit" value="提交"/>
</form>
</body>
</html>


可以使用return false,同时阻止默认行为和冒泡行为。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会编程的阿强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值