jquery中的切换效果不起作用

Sha*_*aoz 1 javascript jquery

请问,为什么这不起作用:

$("#togglePanel").click(function(e){        
    $("#toolpanel").toggle(function(){
        $("#orderMap").animate({marginLeft: 0, width: "100%"}, 500);
    }, function(){
        $("#orderMap").animate({marginLeft: "341px", width: "576px"}, 500);
    });
});
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,它的工作原理是:

$("#togglePanel").click(function(e){        
    $("#toolpanel").toggle(function(){
        $("#orderMap").animate({marginLeft: 0, width: "100%"}, 500);
    });
});
Run Code Online (Sandbox Code Playgroud)

谢谢...

lon*_*day 5

toggle函数"在内部使用click事件".也就是说,它将函数绑定到单击事件,因此您也不需要调用click.试试这个:

$("#togglePanel").toggle(function(){
    $("#orderMap").animate({marginLeft: 0, width: "100%"}, 500);
}, function(){
    $("#orderMap").animate({marginLeft: "341px", width: "576px"}, 500);
});
Run Code Online (Sandbox Code Playgroud)

  • 请注意,`#togglePanel`和`#toolpanel`是两个独立的元素.所以这并不完全等同.这应该仍然基于`#toolpanel`,但是根据Shaoz的最后评论有一个**#togglePanel`的额外**切换来显示/隐藏`#toolpanel`. (3认同)