`
天梯梦
  • 浏览: 13630508 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

jQuery:无限循环两个或者多个事件 click / toggle between two functions

 
阅读更多

插件:

(function($) {
    $.fn.clickToggle = function(func1, func2) {
        var funcs = [func1, func2];
        this.data('toggleclicked', 0);
        this.click(function() {
            var data = $(this).data();
            var tc = data.toggleclicked;
            $.proxy(funcs[tc], this)();
            data.toggleclicked = (tc + 1) % 2;
        });
        return this;
    };
}(jQuery));

 

用法:( 需要jquery文件支持 )

<script type="text/javascript" src="https://code.jquery.com/jquery-1.5.js"></script>
<script>
$(function()
{
    $('div').clickToggle(function() {
        alert('First handler: ' + $(this).text());
    }, function() {
        alert('Second handler: ' + $(this).text());
    });
})
</script>

<div>1. div (click me)</div><br />
<div>2. div (click me)</div>

 

DEMO:http://jsfiddle.net/npwAz/1/

 

Note: It seems that at least since jQuery 1.7, this version of .toggle is deprecated, probably for exactly that reason, namely that two versions exist. Using .toggle to change the visibility of elements is just a more common usage. The method was removed in jQuery 1.9.

 

Update 2: 多事件循环,接受jquery 1.11版本

In the meantime, I created a proper plugin for this. It accepts an arbitrary number of functions and can be used for any event. It can be found on GitHub.

 

可以接受两个或两个以上函数循环执行,方法如下:

The function funcToggle provides the same interface as bind but accepts multiple event handlers:

funcToggle(eventType, [eventData], handler(eventObject),  [handler(eventObject), ...])

This changes the text color on each consecutive click, from to original color to red, to green, to black, to red again and so forth:

element.funcToggle('click', function() {
    $(this).css('color', 'red');
}, function() {
    $(this).css('color', 'green');
}, function() {
    $(this).css('color', 'black');
});

 

Bind handlers for multiple events:

element.funcToggle({
    'click': [function() {
            $(this).css('color', 'red');
        }, function() {
            $(this).css('color', 'green');
        }, function() {
            $(this).css('color', 'black');
        }],
    'mouseover': [function() {
            $(this).css('background-color', 'red');
        }, function() {
             $(this).css('background-color', 'white');
        }]
});

 

下载:jQuery-Function-Toggle-Plugin-master

DEMO: http://sources.ikeepstudying.com/jquery-functions-toggle/

 

更多内容参考:http://stackoverflow.com/questions/4911577/jquery-click-toggle-between-two-functions

本文转自:jQuery:无限循环两个或者多个事件 click / toggle between two functions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    jquery点击改变class并toggle

    jquery点击改变class并toggle

    jQuery显示隐藏密码插件jquery.toggle-password

    jQuery显示隐藏密码插件jquery.toggle-password

    jquery.toggle-password显示密码插件

    jquery.toggle-password显示密码插件

    jQuery复合事件结合toggle()方法的用法示例

    toggle() 方法用于绑定两个或多个事件处理器函数,以响应被选元素的轮流的 click 事件。 语法: $(selector).toggle(function1(),function2(),functionN(),...) 当指定元素被点击时,在两个或多个函数之间轮流切换。...

    toggle-bool:vim插件来切换布尔值

    git clone https://github.com/sagarrakshe/toggle-bool ~/.vim/bundle/toggle-bool 将Bundle 'https://github.com/sagarrakshe/toggle-bool'添加到.vimrc 运行:BundleInstall 将NeoBundle '...

    jQuery toggle事件制作FAQ列表页.zip

    jQuery toggle事件制作FAQ列表页是一款基于jQuery toggle事件实现的FAQ文字列表点击展开和收缩文字内容。

    最土团购商业wap版测试可以

    最土团购商业wap版测试可以 inflating: zuitu/static/img/faq-how-it-works1.gif inflating: zuitu/static/img/faq-how-it-works2.gif inflating: zuitu/static/img/faq-how-it-works3.gif inflating: zuitu/...

    jQuery权威指南-源代码

    不仅书中的每一个小知识点都配有精心选择的小案例(总共100多个),而且还有两个非常实用的综合性案例。所有案例的讲解都非常详细,不仅有功能需求分析和完整实现代码,而且还有最终效果的展示,更重要的是,将所有...

    jquery点击显示更多列表文字.zip

    jquery点击显示更多列表文字是一款jquery toggle事件制作点击显示更多列表文字代码。

    jquery电子文档chm

    Toggle between two function calls every other click. Whenever a matched element is clicked, the first specified function is fired, when clicked again, the second is fired. All subsequent clicks ...

    JQuery入门——事件切换之toggle()方法应用介绍

    1、在toggle()方法中,可以依次调用N个指定的函数,直到最后一个函数,然后重复对这个函数轮番调用,其语法如下:toggle(fn, fn2, [fn3, fn4,…]),其中fn、fn2、fn3、fn4等函数依次调用。 2、示例代码: 代码如下:&...

    jQuery详细教程

    hide() 和 show() 都可以设置两个可选参数:speed 和 callback。 语法: $(selector).hide(speed,callback) $(selector).show(speed,callback) speed 参数规定显示或隐藏的速度。可以设置这些值:"slow", "fast", ...

    jQuery中的时间与动画

    2. 使用bind()方法可以一次性绑定一个或多个事件处理方法,使用unbind()方法可以移除事件绑定 3. 在jQuery中,提供了hover()和toggle()等符合事件方法 4. 在jQuery中,提供了一系列显示动画的方法。其中,使用show()...

    jQuery显示隐藏密码插件jquery.toggle-password.zip

    在 Windows(Windows 7以上)系统以及 IE 里,当输入密码的时候,右边会有一个小眼睛,按下这个小...现在我们可以使用jQuery插件jquery.toggle-password让我们的网站也有类似的功能。 查看演示 标签:jquery

    Atom-toggle-gutter,快速切换原子沟的可见性。为Maximsokolov/Toggle做出贡献.zip

    Atom-toggle-gutter.zip,Quickly toggle visability of Atom's gutters切换边沟,atom是一个用web技术构建的开源文本编辑器。

    微信小程序--实现多个按钮toggle功能

    百度上很多都是只设置一个按钮的toggle,所以我现在来稍微总结下:多个按钮如何实现自身的toggle功能。原理:1,列表展示的时候,我们会用wx:for 来循环数据,那么我们就会得到相 ...       如下图所示,实现该...

    toggle-osx-shadows:用于在OS X上切换窗口阴影的微型工具

    clib install puffnfresh/toggle-osx-shadows 在终端中运行以下命令以关闭OS X窗口阴影。 git clone https://github.com/pufuwozu/toggle-osx-shadows.git cd toggle-osx-shadows make ./toggle-osx-shadows ...

    页面元素绑定jquery toggle后元素隐藏的解决方法

    其实具体原因我也不知道,我通过查找资料才得知,原来jquery在1.9版以后功能发生了变化,不在支持多个事件轮流切换了,搞了半天原来是版本的问题。因此解决办法有两个,目前想到的也就这两个办法: 1、换个稍微低点...

    jQuery 1.3 API 参考文档中文版 html

    click(fn) 常用的事件-&gt;几个事件的顺序 dblclick(fn) 未翻译的翻译一下 focus() 未翻译的翻译一下 scroll(fn 修正 submit(fn) select-&gt;submit toggle() 删除多余的&lt;hr /&gt; stop() 代码注释翻译

Global site tag (gtag.js) - Google Analytics