Ответ 1
Проблема, с которой вы сталкиваетесь, заключается в том, что нет события toggle
; toggle()
- метод jQuery. Чтобы реализовать toggle()
, с on()
, я думаю, вам нужно будет использовать событие click
, а затем оператор if
, чтобы проверить, было ли что-то включенным или переключено/не переписано
$('#wrap').on('click', '#image', function(){
if (!$(this).attr('data-toggled') || $(this).attr('data-toggled') == 'off'){
/* currently it not been toggled, or it 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...
}
});