[jQuery基础] jQuery事件相关
事件绑定
有两种绑定事件方式
- eventName(fn)
编码效率略高/ 部分事件jQuery没有实现,所以不能添加
$(function () {$("button").click(function () {alert("hello lnj")})$("button").click(function () {alert("hello 123")})$("button").mouseleave(function () {alert("hello mouseleave")})$("button").mouseenter(function () {alert("hello mouseenter")})
})
- on(eventName, fn)
编码效率略低/ 所有js事件都可以添加
可以添加多个相同或者不同类型的事件,不会覆盖
$(function () {$("button").on("click", function () {alert("hello click1")})$("button").on("click", function () {alert("hello click2")})$("button").on("mouseleave", function () {alert("hello mouseleave")})$("button").on("mouseenter", function () {alert("hello mouseenter")})
})
事件移除
- off方法如果不传递参数, 会移除所有的事件
$("button").off()
- off方法如果传递一个参数, 会移除所有指定类型的事件
$("button").off("click")
- off方法如果传递两个参数, 会移除所有指定类型的指定事件
("button").off("click", test1)
事件冒泡和默认行为
什么是事件冒泡?
$(function () {$(".son").click(function (event) {alert("son")})$(".father").click(function () {alert("father")})
})
如何阻止事件冒泡
return false
event.stopPropagation()
$(function () {$(".son").click(function (event) {alert("son")// return falseevent.stopPropagation()})$(".father").click(function () {alert("father")})
})
什么是默认行为?
$(function () {$("a").click(function (event) {alert("弹出注册框")})
})
如何阻止默认行为
return false
event.stopPropagation()
$(function () {$("a").click(function (event) {alert("弹出注册框")// return falseevent.preventDefault()})
})
事件自动触发
事件冒泡
trigger: 如果利用trigger自动触发事件,会触发事件冒泡
triggerHandler: 如果利用triggerHandler自动触发事件, 不会触发事件冒泡
$(function () {$(".son").click(function (event) {alert("son")})$(".father").click(function () {alert("father")})$(".father").trigger("click")$(".father").triggerHandler("click")// $(".son").trigger("click")// $(".son").triggerHandler("click")
})
默认行为
trigger: 如果利用trigger自动触发事件,会触发默认行为
triggerHandler: 如果利用triggerHandler自动触发事件, 不会触发默认行为
$(function () {$("input[type='submit']").click(function () {alert("submit")})$("input[type='submit']").trigger("click")$("input[type='submit']").triggerHandler("click")$("span").click(function () {alert("a")})// $("a").triggerHandler("click")$("span").trigger("click")
})
自定义事件
满足条件
事件必须是通过on绑定的
事件必须通过trigger来触发
$(function () {$(".son").on("myClick", function () {alert("son")})$(".son").triggerHandler("myClick")
})
事件命名空间
满足条件
事件是通过on来绑定的
通过trigger触发事件
$(function () {$(".son").on("click.zs", function () {alert("click1")})$(".son").on("click.ls", function () {alert("click2")})// $(".son").trigger("click.zs")$(".son").trigger("click.ls")
})
事件委托
什么是事件委托
请别人帮忙做事情, 然后将做完的结果反馈给我们
在jQuery中,如果通过核心函数找到的元素不止一个, 那么在添加事件的时候,jQuery会遍历所有找到的元素,给所有找到的元素添加事件
$(function () {$(".son").on("click.zs", function () {alert("click1")})$(".son").on("click.ls", function () {alert("click2")})// $(".son").trigger("click.zs")$(".son").trigger("click.ls")
})
事件委托练习
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}html,body{width: 100%;height: 100%;}.mask{width: 100%;height: 100%;background: rgba(0,0,0,0.5);position: fixed;top: 0;left: 0;}.login{width: 521px;height: 292px;margin: 100px auto;position: relative;}.login>span{width: 50px;height: 50px;/*background: red;*/position: absolute;top: 0;right: 0;}</style><script src="js/jquery-1.12.4.js"></script><script>$(function () {// 编写jQuery相关代码$("a").click(function () {var $mask = $("<div class=\"mask\">\n" +" <div class=\"login\">\n" +" <img src=\"images/login.png\" alt=\"\">\n" +" <span></span>\n" +" </div>\n" +"</div>")// 添加蒙版$("body").append($mask);$("body").delegate(".login>span", "click", function () {// 移除蒙版$mask.remove()})return false})})</script>
</head>
<body><!-- <div class="mask"><div class="login"><images src="data:images/login.png" alt=""><span></span></div></div> --><a href="http://www.baidu.com">点击登录</a><div><img src="data:images/06.gif" width="100%" height="100%"></div>
</body>
</html>
移入移出事件
- mouseover/mouseout事件, 子元素被移入移出也会触发父元素的事件
$(function () {$(".father").mouseover(function () {console.log("father被移入了")})$(".father").mouseout(function () {console.log("father被移出了")})
})
- mouseenter/mouseleave事件, 子元素被移入移出不会触发父元素的事件(推荐)
$(function () {$(".father").mouseenter(function () {console.log("father被移入了")})$(".father").mouseleave(function () {console.log("father被移出了")})
})
[jQuery基础] jQuery事件相关相关推荐
- JQuery | JQuery语言 | JQuery基础 | JQuery语言基础
文章目录 前言 一.JQuery语言介绍 一.JQuery版本: 二.JQuery源文件说明: 三.JQuery使用条件: 二.入口函数 一.第一种入口函数 二.第二种入口函数 三.JavaScrip ...
- [jQuery基础] jQuery核心函数和工具方法
核心函数 核心函数 调用jQuery的核心函数 $() jQuery 传递一个函数 $(function () {alert("hello lnj") } 接受一个字符串选择器 返 ...
- [jQuery基础] jQuery事件相关案例 -- 电影排行榜、Tab选项卡
电影排行榜 实现效果展示 实现步骤 第一步(实现静态效果) css部分 *{margin: 0;padding: 0;}.box{width: 300px;height: 450px;margin: ...
- JQuery属性、事件相关操作
JQuery属性相关操作 文章目录 JQuery属性相关操作 一.尺寸相关.滚动事件 1.获取和设置元素的尺寸 2.获取元素相对页面的绝对位置 3.获取浏览器可视区宽度高度 4.获取页面文档的宽度高度 ...
- [jQuery基础] jQuery对象 -- CSS相关
CSS相关 操作CSS 逐个设置 $(function () {$("div").css("width", "100px")$(" ...
- [jQuery基础] jQuery案例 -- qq音乐以及初步解决Ajax 跨域问题
qq音乐案例 案例效果展示 案例效果结构划分 整体布局 歌曲条目部分 顶部栏 底部栏 歌词显示部分 案例实现功能 a. QQ音乐播放器静态页面布局 * 页面整体布局规划和实现 * 页面顶部布局和静态效 ...
- [jQuery基础] jQuery动效
显示和隐藏动画 show() – 显示 hide() – 隐藏 $(function () {// 编写jQuery相关代码$("button").eq(0).click(func ...
- [jQuery基础] jQuery对象 -- 选择器
jQuery对象 选择器 基础选择器 主要有通过id名查询的id选择器.类选择器.标签选择器以及通用选择器 层次选择器 常见有后代选择器.子选择器 .相邻兄弟选择器.同胞选择器 序选择器 等待更新 属 ...
- [jQuery基础] jQuery案例 -- 新浪微博
新浪微博 案例思路 主要说说JavaScript的思路 随时要监听内容的输入 有内容输入 添加,按钮可以点击 没有内容输入 禁止点击按钮 监听点击发布按钮 拿到用户输入的内容 根据内容创建节点 插入微 ...
最新文章
- python解析json
- java orm 工具_GitHub - donnie4w/jdao: jdao是一个java的轻量级orm工具包
- Java 构造方法中super()的作用以及使用
- java switch char_Java7中Switch为什么只支持byte、short、char、int、String
- oracle数据库安装HotSpot,Oracle准备将Java虚拟机 JRockit 和 Hotspot 集成
- Java 数据结构(链表LinkedList增删改查、数组Vector、获取Vector最大值、交换Vector两成员位置、栈的实现、压栈出栈实现反转、队列Queue)
- 智能家居助手后台系统原型/智慧家居后台管理系统/应用分析/页面分析/设备分析/用户管理/运营管理/权限管理/系统设置/问题反馈/商城管理/消息管理/用户画像/公告管理/账号画像/留存用户/数据埋点
- c语言strlren函数的原代码,VB常用函数
- LeetCode之 x 的平方根
- linux 导出insert sql server,【IT爱好者】SQL Server自动生成INSERT语句(在SQL2005下测试通过)...
- html固定表头怎么设置,css如何固定表头
- 小白新建C语言程序(VS2019创建C语言编程环境方法详解)
- MIUI12 Google play无法下载chrome及没有快捷方式的解决方法
- SpringBoot Web开发
- 郭敏:什么是交通事件?如何做好交通事件管理以降低二次事故发生概率?
- 判断当前时间是否在股票开盘时间,不考虑周六周日和节假日
- 小学教育怎么选择特别容易写的论文选题?
- 存储术语中的LUN概念
- Dash中文文档: Lodash
- 小屏隐藏大屏幕显示css,CSS 侧边栏在小屏设备中进行隐藏
热门文章
- php 5.6 mcrypt,php 5.6.36 安装mcrypt
- JDK JRE 区别
- Huawei LiteOS 开发指南
- python回归分析预测模型_在Python中如何使用Keras模型对分类、回归进行预测
- eclipse中svn的各种状态图标详解
- System.gc()与Object.finalize()的区别
- python之路——面向对象进阶
- Linux开启文件共享服务
- svn php 与 apache 的关系 思维导图
- 老人言 摘自云风的blog