读<jquery 权威指南>[2]-事件
1. 事件冒泡
阻止事件冒泡的两种方式:
- event.stopPropagation();
- return false ;
2. 绑定事件——bind(type,[data],function)
type为一个或多个事件类型的字符串,data是作为event.data属性值传递给事件对象的额外对象。
- 一个元素绑定多个事件,可用空格隔开。
$("#btn").bind( "click mouseover", function () { ...);
- 通过映射方式为同一元素绑定多个事件。
$(function () {
$( ".txt").bind({
"focus": function () {
$( "#divTip").html("请输入" ).show();
},
"blur":function () {
$( "#divTip")
.show()
.html( "合法");
}
});
});
- 第二个参数data的使用。
var info = { name: 'Cathy', date: '2014-1-24' };
$(function () {
$("#test").bind("click", info, function (event) {
$("#divTip").show().html(event.data.name + "," + event.data.date);
});
});
3.事件切换
- hover:鼠标悬停与鼠标移除事件切换。
$(function () {
$(".clsTitle").hover(
function () {
$(".clsContent").show();
},
function () {
$(".clsContent").hide();
});
});
- toggle:依次顺序调用N个函数,最后一个调用完成后再从第一个轮流执行。
$(function () {
$("#divTest").toggle(
function () {
alert(1);
},
function () {
alert(2);
},
function () {
alert(3);
}
);
});
4.移除事件——unbind(type,func)
参数说明:type为要移除的事件类型,func为要移除的事件处理函数。如果func为空,则移除元素所有的事件。
function func() {
$("#divTip").append("点击按钮2");
}
$(function () {
$("#Button1").click(function () {
$("#divTip").append("点击按钮1");
});
$("#Button2").click(func);
$("#Button3").click(function () {
$("input").unbind("click", func);
});
});
5.其他事件
one(type,[data],func)——为元素绑定只执行一次的事件。
trigger(type,[data])——在所选择的元素上触发指定类型的事件。
$(function () {
var i = 1;
function btn_Click() {
this.value = i++;
}
$( "input").one("click" , btn_Click);
$( "input").bind("click" , btn_Click);
$( "input").trigger("click" );
})
6.实例应用
①选项卡效果
<body> <ul id="menu"> <li class="tabFocus">家居 </li> <li> 电器</li > <li> 二手</li > </ul> <ul id="content"> <li class="conFocus">我是家居的内容 </li> <li> 欢迎您来到电器城 </li> <li> 二手市场,产品丰富多彩 </li> </ul> </body>
html Body
<script type="text/javascript">
$( function () {
$( "#menu li").each(function (index) {
$( this).click(function () {
$( this).addClass("tabFocus" ).siblings().removeClass("tabFocus");
$( "#content li:eq(" + index + ")" ).show().siblings().hide();
});
});
});
</script>
②屏幕中间弹窗遮罩
<style type="text/css">
body {
font-size: 13px;
} .divShow {
line-height: 32px;
height: 32px;
background-color: #eee;
width: 280px;
padding-left: 10px;
} .divShow span {
padding-left: 50px;
} .dialog {
width: 360px;
border: solid 5px #666;
position: absolute;
display: none;
z-index: 101;
} .dialog .title {
background-color: #fbaf15;
padding: 10px;
color: #fff;
font-weight: bold;
} .dialog .title img {
float: right;
} .dialog .content {
background-color: #fff;
padding: 25px;
height: 60px;
} .dialog .content img {
float: left;
} .dialog .content span {
float: left;
padding-top: 10px;
padding-left: 10px;
} .dialog .bottom {
text-align: right;
padding: 10px 10px 10px 0px;
background-color: #eee;
} .mask {
width: 100%;
height: 100%;
background-color: #000;
position: absolute;
top: 0px;
left: 0px;
filter: alpha(opacity=30);
display: none;
z-index: 100;
} .btn {
border: #666 1px solid;
padding: 2px;
width: 65px;
filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffff, EndColorStr=#ECE9D8);
}
</style>
<div class="divShow">
<input id="Checkbox1" type="checkbox" />
<a href="#">这是一条可删除的记录</a>
<span>
<input id="Button1" type="button" value="删除" class="btn" />
</span>
</div>
<div class="mask"></div>
<div class="dialog">
<div class="title">
<img src="Images/close.gif" alt="点击可以关闭" />删除时提示
</div>
<div class="content">
<img src="Images/delete.jpg" alt="" /><span>您真的要删除该条记录吗?</span>
</div>
<div class="bottom">
<input id="Button2" type="button" value="确定" class="btn"/>
<input id="Button3" type="button" value="取消" class="btn"/>
</div>
</div>
html
<script type="text/javascript">
$(function () {
$("#Button1").click(function () { //注册删除按钮点击事件
$(".mask").show(); //显示背景色
showDialog(); //设置提示对话框的Top与Left
$(".dialog").show(); //显示提示对话框
})
/*
*根据当前页面与滚动条位置,设置提示对话框的Top与Left
*/
function showDialog() {
var objW = $(window); //当前窗口
var objC = $(".dialog"); //对话框
var brsW = objW.width();
var brsH = objW.height();
var sclL = objW.scrollLeft();
var sclT = objW.scrollTop();
var curW = objC.width();
var curH = objC.height();
//计算对话框居中时的左边距
var left = sclL + (brsW - curW) / 2;
//计算对话框居中时的上边距
var top = sclT + (brsH - curH) / 2;
//设置对话框在页面中的位置
objC.css({ "left": left, "top": top });
} $(window).resize(function () {//页面窗口大小改变事件
if (!$(".dialog").is(":visible")) {
return;
}
showDialog(); //设置提示对话框的Top与Left
}); $(".title img").click(function () { //注册关闭图片点击事件
$(".dialog").hide();
$(".mask").hide();
}) $("#Button3").click(function () {//注册取消按钮点击事件
$(".dialog").hide();
$(".mask").hide();
}) $("#Button2").click(function () {//注册确定按钮点击事件
$(".dialog").hide();
$(".mask").hide();
if ($("input:checked").length != 0) {//如果选择了删除行
$(".divShow").remove(); //删除某行数据
}
})
})
</script>
读<jquery 权威指南>[2]-事件的更多相关文章
- 读<jquery 权威指南>[1]-选择器及DOM操作
今天是小年了,2013马上要过去了,但是学习不能间断啊.最近正在看<jQuery权威指南>,先温习一下选择器和DOM操作. 一.基本选择器 1.table单双行: $(function ( ...
- 读<jquery 权威指南>[3]-动画
一. 显示与隐藏——hide(),show() 1. 方法: hide(speed,[callback]); show(speed,[callback]); 说明:这两个方法还可以实现带动画效果的显示 ...
- 读<jquery 权威指南>[4]-Ajax
一.获取异步数据 jQuery可以从服务器异步获得静态数据. ①load() $.load(url,data,callback) url要加载的页面地址, data发送到服务器的数据key/value ...
- 读<jQuery 权威指南>[6]--实用工具函数
官方地址:http://api.jquery.com/category/utilities/ 一.数组和对象操作 1. $.each——遍历 $.each(obj,function(param1,pa ...
- 读<jQuery 权威指南>[5]-插件
一.说明 jQuery插件官网:http://plugins.jquery.com/ 使用插件时引用顺序:插件引用要位于主jquery库之后. 二.插件应用实例 演示插件jquery.validate ...
- 读<jquery 权威指南>[7]-性能优化与最佳实践
一.优化选择器执行速度 1. 优先使用ID选择器和标记选择器 使用选择器时应该首选ID选择器($("#id")),其次是标记选择器($("div")),最后再选 ...
- 跟小静读《jQuery权威指南》——目录
前言 2014年开始了,年底给自己制订的学习计划,第一步先从学习<jQuery权威指南>开始. jQuery大家都很比较熟悉,但是我经常是边用的时候边对照着API,这次找本书通读一遍,记录 ...
- 《jQuery权威指南》学习笔记之第2章 jQuery选择器
2.1 jQuery选择器概述 2.1.1 什么使选择器 2.1.2 选择器的优势: 代码更简单,完善的检测机制 1.代码更简单 示例2-1 使用javascript实现隔行变色 < ...
- [在读] javascript权威指南第六版
耽搁了有大半年没看,记得当时看到5分之2了吧.权威指南是不管读几遍都能觉得有新收获的书^^
随机推荐
- 前向后瞻正则表达式及其JS例子
定义 x(?=y) 匹配'x'仅仅当'x'后面跟着'y'.这种叫做正向肯定查找. 比如,/Jack(?=Sprat)/会匹配到'Jack'仅仅当它后面跟着'Sprat'./Jack(?=Sprat|F ...
- Java基础之扩展GUI——高亮元素、上下文菜单、移动旋转元素、自定义颜色(Sketcher 10)
窗口应用程序. 本例在上一版的基础上实现了高亮元素.移动元素.上下文菜单.旋转元素.设置自定义颜色. 1.自定义常量包: // Defines application wide constants p ...
- Quartz.NET
http://www.360doc.com/userhome.aspx?userid=11741424&cid=2#
- leetcode_199 Binary Tree Right Side View
题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the ...
- BAT脚本打印空行的使用方法
@echo off echo= echo, echo; echo+ echo/ echo[ echo] echo: echo. echo\ pause 这十种方法可以分为三组,每组的效率依次递减. 至 ...
- 《zw版·Halcon-delphi系列原创教程》 只有2行代码的超市收款单ocr脚本
<zw版·Halcon-delphi系列原创教程> 只有2行代码的超市收款单ocr脚本只有2行代码的超市收款单ocr脚本 发了这么多教程,有网友问,为什么没有ocr的. 的确,在 ...
- BeauifulSoup学习使用记录
BeautifulSoup的安装很简单pip install BeautifulSoup4 相关信息链接http://cuiqingcai.com/1319.html
- 【git学习】sha1 deflate
deflate has rfc sha1 has rfc sha1和md5sum类似,可以学习整理
- SQL标签
SQL标签库提供了与关系型数据库进行交互的标签. 引入语法:<%@ taglib prefix="sql" uri="http://java.sun.com/jsp ...
- Hadoop日志文件分析系统
Hadoop日志分析系统 项目需求: 需要统计一下线上日志中某些信息每天出现的频率,举个简单的例子,统计线上每天的请求总数和异常请求数.线上大概几十台 服务器,每台服务器大概每天产生4到5G左右的日志 ...