这里的快速问题,我似乎无法得到这个
$('#wrap').on('toggle','#image',function(){
<!-- Do stuff -->
});
能够在其中有一个切换吗?有任何想法吗?我尝试过谷歌,但没有运气.
这是我正在尝试使用的代码.on,因为目前它不适用于所有元素子页面的更改(具有#image和#brick)
$("#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");
});
});
解决方法
你所面临的问题是没有切换事件; toggle()是一个jQuery方法.要实现一个toggle(),使用on()我认为你需要使用一个点击事件,然后一个if语句来测试某些东西是被切换,还是切换/不切换
$('#wrap').on('click',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...
}
});