首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.on()切换协同工作(&T)

.on()切换协同工作(&T)
EN

Stack Overflow用户
提问于 2012-06-03 01:13:45
回答 1查看 14.9K关注 0票数 10

这里有个小问题,伙计们,我好像不明白

代码语言:javascript
复制
$('#wrap').on('toggle', '#image', function(){
    <!-- Do stuff -->
  });

才能在里面有一个开关?有什么想法吗?我试过用谷歌搜索,但没有成功。

这是我试图使用.on的代码,因为目前它不会将更改应用于页面上的所有元素,(具有#image和#brick)

代码语言:javascript
复制
$("#result_options").toggle(function() {
        var image_width = $('#image').width() / 2;
        var brick_width = $('#brick').width() / 2;
        $("#image").css("width",image_width);
        $("#image").css("padding","4px");
        $("#brick").css("width",brick_width);
    },function (){
        $("#image").css("width","300");
        $("#image").css("padding","8px");
        $("#brick").css("width","314");
        $(this).html("Smaller Results");
    });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-03 01:21:27

您面临的问题是没有toggle事件;toggle()是一个jQuery方法。要使用on()实现toggle(),我认为您需要使用一个click事件,然后使用一个if语句来测试某些东西是否已被切换或关闭/未切换

代码语言:javascript
复制
$('#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...
    }
});
票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10864353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档