我有一個打開一個小窗口來顯示額外信息的按鈕。 目前它只打開,我不得不點擊關閉按鈕關閉它,我真的不希望這個關閉按鈕,但我希望能夠打開和關閉它與同一個按鈕。jquery打開並關閉同一個按鈕
$(function() {
//----- OPEN
$('[data-popup-open]').on('click', function(e) {
var targeted_popup_class = jQuery(this).attr('data-popup-open');
$('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);
e.preventDefault();
});
//----- CLOSE
$('[data-popup-close]').on('click', function(e) {
var targeted_popup_class = jQuery(this).attr('data-popup-close');
$('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);
e.preventDefault();
});
});
任何人都可以幫助我嗎?
因爲點擊事件綁定到當時存在的項目,所以應該使用事件委派。 –
也許你應該使用'.toggle'來代替。 –
@ DanielA.White懸停在日曆中的for循環中創建,它們可能會也可能不會被創建,具體取決於當天有多少事件,所以我需要它來綁定到項目。 –