jquery-10 jquery中的绑定事件和解绑事件的方法是什么
jquery-10 jquery中的绑定事件和解绑事件的方法是什么
一、总结
一句话总结:bind(); unbind(); one();
1、 jquery中的绑定事件和解绑事件的方法是什么?
bind(); unbind();
i=0;
$('button').eq(0).click(function(){
$('img').bind('click',function(){
if(i%2==0){
this.src='b.png';
}else{
this.src='a.png';
}
i++;
});
}); $('button').eq(1).click(function(){
// $('img').unbind('click');
$('img').unbind();
});
2、one()方法是什么意思?
给元素绑定事件,但是这个事件执行一次就消失
$('img').one('click',function(){
3、bind(); unbind(); one();方法的参数是什么?
第一个参数是事件的字符串,第二个参数是匿名函数
$('img').one('click',function(){
$('img').bind('click',function(){
4、点赞功能有什么注意事项?
前端和后端都要实现点赞了就不能再点了的效果
i=0;
$('button').click(function(){
if(i==0){
val=parseInt($('span').html())+1;
$('span').html(val);
$(this).css({'background':'#00f','color':'#fff'});
}else{
alert('您已经点过赞了,不要贪杯噢!');
}
i++;
});
二、jquery中的绑定事件和解绑事件的方法是什么
1、相关知识
2.事件处理
bind();
unbind();
one();
[实例:点赞功能]
[实例:鼠标拖动功能]
2、代码
只绑定一次事件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
margin:0px;
padding:0px;
}
</style>
<script src="jquery.js"></script>
</head>
<body>
<div>
<img src="a.png" alt="">
</div>
<br>
<button>添加单击事件</button>
<button>删除单击事件</button>
</body>
<script>
i=0;
$('button').eq(0).click(function(){
$('img').one('click',function(){
if(i%2==0){
this.src='b.png';
}else{
this.src='a.png';
}
i++;
});
});
</script>
</html>
绑定事件和解除绑定事件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
margin:0px;
padding:0px;
}
</style>
<script src="jquery.js"></script>
</head>
<body>
<div>
<img src="a.png" alt="">
</div>
<br>
<button>添加单击事件</button>
<button>删除单击事件</button>
</body>
<script>
i=0;
$('button').eq(0).click(function(){
$('img').bind('click',function(){
if(i%2==0){
this.src='b.png';
}else{
this.src='a.png';
}
i++;
});
}); $('button').eq(1).click(function(){
// $('img').unbind('click');
$('img').unbind();
});
</script>
</html>
click实现点赞功能
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
margin:0px;
padding:0px;
}
</style>
<script src="jquery.js"></script>
</head>
<body>
<div>
<img src="a.png">
</div>
<br>
<button>点赞(<span>50</span>)</button>
</body>
<script>
i=0;
$('button').click(function(){
if(i==0){
val=parseInt($('span').html())+1;
$('span').html(val);
$(this).css({'background':'#00f','color':'#fff'});
}else{
alert('您已经点过赞了,不要贪杯噢!');
}
i++;
});
</script>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
margin:0px;
padding:0px;
}
</style>
<script src="jquery.js"></script>
</head>
<body>
<div>
<img src="a.png" alt="">
</div>
<br>
<button>添加单击事件</button>
<button>删除单击事件</button>
</body>
<script>
i=0;
$('button').eq(0).click(function(){
$('img').one('click',function(){
if(i%2==0){
this.src='b.png';
}else{
this.src='a.png';
}
i++;
});
});
</script>
</html>
jquery-10 jquery中的绑定事件和解绑事件的方法是什么的更多相关文章
- js进阶---12-10、jquery绑定事件和解绑事件是什么
js进阶---12-10.jquery绑定事件和解绑事件是什么 一.总结 一句话总结:on和off. 1.jquery如何给元素绑定事件? on方法 22 $('#btn1').on('click', ...
- js绑定事件和解绑事件
在js中绑定多个事件用到的是两个方法:attachEvent和addEventListener,但是这两个方法又存在差异性 attachEvent方法 只支持IE678,不兼容其他浏览器 addEv ...
- JS---DOM---为元素绑定事件和解绑事件的兼容代码
1. 绑定事件的兼容 function addEventListener(element,type,fn) { if(element.addEventListener){ element.addEve ...
- jq 绑定事件和解绑事件
<!DOCTYPE html><html><head> <script src="http://cdn.static.runoob.com/libs ...
- JS添加事件和解绑事件:addEventListener()与removeEventListener()
作用: addEventListener()与removeEventListener()用于处理指定和删除事件处理程序操作. 它们都接受3个参数:事件名.事件处理的函数和布尔值. 布尔值参数是true ...
- python 全栈开发,Day55(jQuery的位置信息,JS的事件流的概念(重点),事件对象,jQuery的事件绑定和解绑,事件委托(事件代理))
一.jQuery的位置信息 jQuery的位置信息跟JS的client系列.offset系列.scroll系列封装好的一些简便api. 一.宽度和高度 获取宽度 .width() 描述:为匹配的元素集 ...
- 原生js绑定和解绑事件,兼容IE,FF,chrome
主要是最近项目中用到了原生的js 解绑和绑定 事件 然后今天研究了一下,其实问题不大,不过要注意不要把单词写错了,今天我就找了好久单词写错了. 需求:当鼠标移上去以后,给Select加载元素,接着解 ...
- jquery 事件的绑定,触发和解绑
js和jquery绑定的区别? HTML或原生js是单一对应绑定的,绑多了只留最后一个.jQuery是追加绑定的,绑多少执行多少.这个在每一本jQuery的书中都是首先提到的事情. jquery绑定与 ...
- jquery绑定事件,解绑事件
unbind([type],[data]) 是 bind()的反向操作,从每一个匹配的元素中删除绑定的事件.如果没有参数,则删除所有绑定的事件.你可以将你用bind()注册的自定义事件取消绑定.如果提 ...
随机推荐
- 杭电(hdu)2053 Switch Game 水题
Switch Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 控制面板项 .cpl 文件说明
控制面板项 .cpl 文件说明 appwiz.cpl 程序和功能.卸载或更改程序 bthprops.cpl 蓝牙控制面板 desk.cpl ...
- error: function declaration isn’t a prototype [-Werror=strict-prototypes]
"warning: function declaration isn't a prototype" was caused by the function like that: ...
- Direct2D 如何关闭抗锯齿
// Each pixel is rendered if its pixel center is contained by the geometry. // D2D1_ANTIALIAS_MODE_A ...
- CentOS不能进入登录界面
http://blog.csdn.net/powerzone/article/details/6798646
- 洛谷 P1308 统计单词数
P1308 统计单词数 题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定 ...
- asp.net Code学习一(vs code跨平台软件操作)
1.命令行: dotnet new -t web 创建web项目 dotnet new restore build pubilsh run test pack dotnet -info / -h do ...
- LinearLayout-layout_gravity 属性没有效果分析
今天在一个布局文件中,遇到了一个问题,先看代码 <LinearLayout android:layout_width="match_parent" android:layou ...
- DBLINK做系统集
过度使用DBLINK做系统集成会带来的问题 过度使用DBLINK做系统集成会带来很多问题,问题主要由以下几点: 1. 大量消耗数据库资源: 本地系统每通过DBLINK链接远端系统一次,都会生成一个本地 ...
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第三篇:ASP.NET MVC全局观
摘要 本文对ASP.NET MVC的全局运行机理进行一个简要的介绍,以使得朋友们更好的理解后续文章. 前言 在上一篇文章中,我们实现了第一个ASP.NET MVC页面.对于没有接触 ...