2012-01-16 94 views
0
if($(this).parents("ul").closest("#menu-main").length){ 
    var parent = $(this).parent(); 

    parent.addClass("current"); 

    if(parent.hasClass("more-options")){ 
    $(parent).siblings(".more-options-page").addClass("current").show(); 
    } 
} 

Instad使用if語句重複addClass(),如何將show()添加到第一個類的「more-options」類的父級?jQuery:如何將這兩個語句合併爲一個?

回答

1

如果你願意,你可以鏈上的所有必要的方法調用像這樣(提供可選的過濾器the parent method):

if ($(this).parents("ul").closest("#menu-main").length) { 
    $(this).parent().addClass("current"); 
    $(this).parent(".more-options").show().siblings(".more-options-page").addClass("current").show(); 
} 

更新

是的,它可以在一個線路來完成,但不是完全可讀的:

if ($(this).parents("ul").closest("#menu-main").length) { 
    $(this).parent().addClass("current").filter(".more-options").show().siblings(".more-options-page").addClass("current").show(); 
} 
+0

反正有鏈接在一個而不是兩個單獨的聲明嗎? – Bongbong 2012-01-16 12:25:11

+0

@豐寶看更新! – 2012-01-16 12:36:27

+0

好的,謝謝你的提示,Rich。 – Bongbong 2012-01-16 13:29:54

相關問題