jQuery事件绑定与解绑的方式
jQuery绑定事件的方式很简单,有两种。其优异点都在下面的注释中做好了,请看下面的代码吧。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery事件绑定</title>
<script src="js/jquery.js"></script>
<script> $(function(){ /* jQuery中有两种绑定事件方式 1.eventName(fn) 编码效率高/部分事件jQUery没有实现,所以不能添加 2.on(eventName,fn) 编码效率低/所有js事件都可以添加 注意点: 可以添加多个相同或者不同的事件,不会覆盖 */ $("button").click(function(){ alert("hello world1") }); $("button").click(function(){ alert("hello 1234") }); $("button").mouseleave(function(){ alert("hello mouseleave") }); $("button").mouseenter(function(){ alert("hello mouseenter") }); // $("button").on("click",function(){ // alert("hello world2") // }) // off方法如果不传递参数,会移除所有的事件 // off方法如果传递一个参数,会移除所有指定类型的事件 // off方法如果传递两个参数,会移除所有指定类型的指定事件 }) </script>
</head>
<body>
<button>我是按钮</button>
</body>
</html>
大家可通过控制台来查看其中的运行效果,若有任何疑问或是不解,请在下方评论,谢谢。
还没有评论,来说两句吧...