jQuery on('toggle')可能吗? [英] jQuery on('toggle') possible?

查看:64
本文介绍了jQuery on('toggle')可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是使用 $('。element')。toggle(function(){},function(){});

我需要在我想要使用的元素上使用 .on()方法 .toggle()但我无法弄清楚如何指定我的第二个事件处理函数:

I would need to use the .on() method on the element I want to use .toggle() on but I can't figure out how to specify my second event handler function:

$('body').on('toggle', '.element', function(){});

我必须在哪里为切换事件指定第二个处理函数?

Where would I have to specify the second handler function for my toggle event?

我这样做了: http://jsfiddle.net/D33f4/ 但第一次点击没有任何反应。我真的不明白为什么。

I did this here: http://jsfiddle.net/D33f4/ but nothing happens on the first click. I really don't understand why.

推荐答案

尝试只使用切换。

$('#clickme').toggle(
    function(){alert('click 1');},
     function(){alert('click 2');}
);

实际上,它在第一次点击时没有做任何事情,因为它只附加事件处理程序那时。

As it is, it doesn't do anything on the first click because it's only attaching the event handler at that point.

编辑:

我没有完全阅读帖子。我很糟糕。

I didn't fully read the post. My bad.

$('body').on('click', '#clickme', function()
{
    var state = $(this).data('state');


    switch(state){
        case 1 :
        case undefined : alert('click 1'); $(this).data('state', 2); break;
        case 2 : alert('click 2'); $(this).data('state', 1); break;
    }
});

这篇关于jQuery on('toggle')可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆