2015-11-02 72 views
0

HTML代碼我不能上課

​​

JS代碼

Template.pI.events({ 
     'click .expandOrCollapse':function(event){ 
     event.preventDefault(); 
     $(event.target).closest().toggleClass("contentMain Big"); 
     } 
    }); 

CSS代碼

.contentMain{ 
    position: relative; 
    overflow: hidden; 
    height: 60px; 
    top: -15px; 
    font-family: Palatino Linotype, Georgia, Verdana, sans-serif; 
} 

.Big{ 
    position: relative; 
    height: auto; 
    top: -15px; 
    font-family: Palatino Linotype, Georgia, Verdana, sans-serif; 
} 

我希望類 「contentMain」 切換到「大之間切換「當我點擊」expandOrCollapse「時,當我點擊」expandOrCollapse「時,當前類是」contentMain「,類」Big「切換到」contentMain「目前的課程是「大」。我不認爲我對JS代碼有任何錯誤。 任何幫助和建議將高度讚賞

回答

7

因爲你正在尋找的元素不是點擊的元素的祖先,而不是點擊的元素的父級的前一個兄弟

$(event.target).parent().prev().toggleClass("contentMain Big"); 

時,使用closest()方法找到匹配選擇器的第一個祖先元素

+0

非常感謝。你的回答解決了我的問題。非常感謝你 –

+0

@SujoyDe,如果回答你的問題,請接受回答 – Sasikanth

+0

我接受 –

相關問題