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中的绑定事件和解绑事件的方法是什么的更多相关文章

  1. js进阶---12-10、jquery绑定事件和解绑事件是什么

    js进阶---12-10.jquery绑定事件和解绑事件是什么 一.总结 一句话总结:on和off. 1.jquery如何给元素绑定事件? on方法 22 $('#btn1').on('click', ...

  2. js绑定事件和解绑事件

    在js中绑定多个事件用到的是两个方法:attachEvent和addEventListener,但是这两个方法又存在差异性 attachEvent方法  只支持IE678,不兼容其他浏览器 addEv ...

  3. JS---DOM---为元素绑定事件和解绑事件的兼容代码

    1. 绑定事件的兼容 function addEventListener(element,type,fn) { if(element.addEventListener){ element.addEve ...

  4. jq 绑定事件和解绑事件

    <!DOCTYPE html><html><head> <script src="http://cdn.static.runoob.com/libs ...

  5. JS添加事件和解绑事件:addEventListener()与removeEventListener()

    作用: addEventListener()与removeEventListener()用于处理指定和删除事件处理程序操作. 它们都接受3个参数:事件名.事件处理的函数和布尔值. 布尔值参数是true ...

  6. python 全栈开发,Day55(jQuery的位置信息,JS的事件流的概念(重点),事件对象,jQuery的事件绑定和解绑,事件委托(事件代理))

    一.jQuery的位置信息 jQuery的位置信息跟JS的client系列.offset系列.scroll系列封装好的一些简便api. 一.宽度和高度 获取宽度 .width() 描述:为匹配的元素集 ...

  7. 原生js绑定和解绑事件,兼容IE,FF,chrome

    主要是最近项目中用到了原生的js 解绑和绑定 事件  然后今天研究了一下,其实问题不大,不过要注意不要把单词写错了,今天我就找了好久单词写错了. 需求:当鼠标移上去以后,给Select加载元素,接着解 ...

  8. jquery 事件的绑定,触发和解绑

    js和jquery绑定的区别? HTML或原生js是单一对应绑定的,绑多了只留最后一个.jQuery是追加绑定的,绑多少执行多少.这个在每一本jQuery的书中都是首先提到的事情. jquery绑定与 ...

  9. jquery绑定事件,解绑事件

    unbind([type],[data]) 是 bind()的反向操作,从每一个匹配的元素中删除绑定的事件.如果没有参数,则删除所有绑定的事件.你可以将你用bind()注册的自定义事件取消绑定.如果提 ...

随机推荐

  1. RelativeLayout-代码中成员控件width height

    今天需要在代码中动态的设置一个textview的width跟height属性,记录下来. textview在xml中的布局如下 <RelativeLayout android:layout_wi ...

  2. 【单词】常见单词含义的辨异(emulator/simulator、hardware/firmware)

    1. emulator 与 simulator The Simulator tries to duplicate the behavior of the device.(仿真的是行为): The Em ...

  3. Objective-C基础笔记(7)Foundation中的常用结构体

    一.NSRange NSRange的定义: typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; N ...

  4. go语言函数作为参数传递

    go语言函数作为参数传递,目前给我的感觉几乎和C/C++一致.非常的灵活. import "fmt" import "time" func goFunc1(f ...

  5. 如何使用maven 打包源代码呢?

    如何使用maven 打包源代码呢? http://hw1287789687.iteye.com/blog/1943157

  6. 洛谷 P1626 象棋比赛

    P1626 象棋比赛 题目描述 有N个人要参加国际象棋比赛,该比赛要进行K场对弈.每个人最多参加两场对弈,最少参加零场对弈.每个人都有一个与其他人不相同的等级(用一个正整数来表示). 在对弈中,等级高 ...

  7. [TS] Implement a doubly linked list in TypeScript

    In a doubly linked list each node in the list stores the contents of the node and a pointer or refer ...

  8. onWindowFocusChanged-屏幕焦点函数回调情况

    1.这个函数的具体作用不太清楚,但网上有人说是 ,当activity得到或者失去焦点的时候,就会调用这个方法 先看如下代码 @Override public void onWindowFocusCha ...

  9. [置顶] WebService学习总结(1)——WebService相关概念

    一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...

  10. RMAN异机复制数据库(相同路径)

    有完整的备份,新的数据库datafile.controfile.logfile所在目录结构和原数据库一样. 创建好adump.bdump.cdump.udump等目录. 1.恢复参数文件. 设置环境变 ...