2016-12-24 77 views
0

我有類detail的箭頭,點擊事件後,我添加class'close'。爲jquery動態添加類的功能

如何設置close這個箭頭的功能?

var arrow = document.querySelector('.arrow-down.detail'); 
var imgs = document.querySelectorAll('div.img'); 
for (i = 0; i < imgs.len; i++) { 
    imgs[i].onclick = function(e) { 
     this.classList.add('active'); 
     arrow.style.transform = 'translate(-50%, -50%) rotate(90deg)'; 
     arrow.style.color = '#000'; 
     arrow.style.borderColor = '#000'; 
     arrow.classList.add('close'); 
    } 
}; 

我需要的是這樣的:

$('.detail.close').click(function(){ 
}) 
+0

目前尚不清楚您需要什麼。 –

+0

我需要用關閉'img'塊的'close'來爲箭頭設置函數。 –

+0

實際上,我忘了寫'this.classList.add('active')' –

回答

0

如果你讓這樣的代碼下面,它會聽body元素的所有點擊事件,但會調用回調是它來自」 .detail。當下面的代碼被觸發時,'只''身體'必須存在。它會解決你的問題。

$('body').on('click', '.detail.close', function(){ 
    // do sth 
}); 

快樂黑客和節日快樂!