首页 前端知识 jQuery事件处理

jQuery事件处理

2024-03-12 01:03:32 前端知识 前端哥 791 999 我要收藏

jQuery的事件处理

1.页面载入事件

$(document).ready() -- onload
复制

2.事件绑定(bind)

bind(type,[data],fn)
复制
  • type:表示事件类型(click、mouseover、mouseout…)
  • [data]:可选参数,表示传递给事件对象的额外数据
  • fn:是一个函数,(事件处理函数),当事件发生时执行的函数。
<body>
<button id="btn">确定</button>
</body>
<script>
$(function(){
$("#btn").bind("click",function(){
alert("事件绑定");
})
})
</script>
复制

3.反绑定事件(unbind)

unbind([type],[data]):删除绑定的事件
复制
  1. 不带参数:删除元素上绑定的所有事件
  2. 带参数:[type]表示事件类型

4.一次性绑定事件(one)

一次性绑定事件(one):绑定的事件只能执行一次

<body>
<img src="https://blog.csdn.net/weixin_62343228/images/p1.jpg" alt="" width="300" height="180">
</body>
<script>
$(function(){
//通过鼠标的悬停、离开事件改变img的图像
$("img").bind("mouseover",function(){
$("img").attr({
src:"../../images/p2.jpg", //this表示的是当前img
})
})
// $("img").bind("mouseout",function(){
// $(this).attr({
// src:"https://blog.csdn.net/weixin_62343228/images/p1.jpg",
// })
// })
// $("img").unbind("mouseout");
$("img").one("mouseout",function(){
$(this).attr({
src:"https://blog.csdn.net/weixin_62343228/images/p1.jpg",
})
})
})
</script>
复制

5.模拟鼠标悬停(hover)

<body>
<div style="width: 200px; height: 200px;
复制
转载请注明出处或者链接地址:https://www.qianduange.cn//article/3693.html
标签
评论
发布的文章

JavaScript基础库

2024-04-09 00:04:11

解决vue npm 下载echart出错

2024-04-09 00:04:05

Echarts中option属性设置

2024-04-08 23:04:58

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!