.on() & toggle working together

╄→гoц情女王★ 提交于 2019-11-28 08:37:13
David Thomas

The problem you're facing is that there is no toggle event; toggle() is a jQuery method. To implement a toggle(), with on() I think you'd need to use a click event, and then an if statement to test whether something's been toggled on, or toggled-off/not-toggled

$('#wrap').on('click', '#image', function(){
    if (!$(this).attr('data-toggled') || $(this).attr('data-toggled') == 'off'){
        /* currently it's not been toggled, or it's been toggled to the 'off' state,
           so now toggle to the 'on' state: */
           $(this).attr('data-toggled','on');
           // and do something...
    }
    else if ($(this).attr('data-toggled') == 'on'){
        /* currently it has been toggled, and toggled to the 'on' state,
           so now turn off: */
           $(this).attr('data-toggled','off');
           // and do, or undo, something...
    }
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!