html 元素 关于事件绑定一些结论

实验了三种形式绑定事件的方法-以绑定click事件为例子。
一个使用jQuery的方式,一个使用js原生,一个直接在html元素利用onclick属性绑定。
实践的结论:

  1. 当jQuery方式绑定的事件,执行顺序在 直接在html上绑定的 优先级低。
  2. 当jQuery方式绑定的事件 与 js原生方式绑定的事件 的优先级判定在于哪一个
    先绑定,如果jQuery先进行绑定,则会执行完jQuery方式绑定的所有方式。再执行js绑定的方法。
  3. 使用js绑定的方法会覆盖html元素绑定的方法,而jQuery不会覆盖html方式绑定的onclick的方法。

    为什么js能够覆盖 html方式绑定的onclick方法,而jQuery没有?
    因为js改变了html 的 onclick 属性。
    jQuery绑定事件的方式没有利用onclick的属性。

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
document.getElementById("but").onclick=function (){alert(99999);};
$("button").click(function(){
alert(111);
});

$("button").click(function(){
alert(2);
alert(document.getElementById("but").onclick);
});
$("button").click(function(){
alert(3);
});
$("button").click(function(){
alert(4);
});
$("div").click(function(){
alert(5555);
});

});
function change(){
alert(33333);
}
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<div>
<button id="but" type="button" onclick="change()">Click me</button>
</div>
</body>
</html> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值