2011-11-27 34 views
1

以下是JavaScript的除外。變化讓孩子在jQuery中擁有GrandChildren

 
    function showContent(layer, animate) { 

     $("#background").show(); 
     $("footer").hide(); 
     $("#announcement").hide(); 

     $("#content").html($("#" + layer + "_html").comments()); 

     $("nav").children().each(function() { 
      if ($(this).attr("id").search(layer) == -1) { 
       $(this).addClass("inactive"); 
      } else { 
       $(this).removeClass("inactive"); 
      } 
     }); 

     if (animate == true) { 
      $('html,body').animate({scrollTop: $("nav").offset().top - 16 - 40}, 200, "swing"); 
     } 

     // setUpHover(); 

     setupFullScreen(); 
     setupImageViewers(); 
     setupHovers(); 

     // Load analytics tracking 
     setTimeout(function() { 
      $("#tracker").attr("src", "/track/" + layer + "/") 
     }, 500); 

     window.location.hash = layer; 
    } 

    // Set up the on click functions for the different menus 
    $("nav").children().click(function() { 
     showContent($(this).attr("id").split("_")[1], true); 
    }); 

這裏是HTML的摘錄:

http://pastie.org/2929494

現在,這裏是我的問題。有了這個js和html,它就可以做到這一點。然而,當我在導航裏面添加一個需要DIV這樣它打破:

http://pastie.org/2929497

我想這個問題是在原JS以上,大約有(「NAV」)兩行兒童和想象。我的新代碼,他們現在實際上是孫子或其他人。所以很可能需要對這兩條線進行微小的調整,但不知道該如何調整。

在此先感謝!

+0

用'find'替換'children'。 –

+0

這太有趣了,我剛剛根據我的發現在您的評論之前嘗試過:http://www.hackosis.com/jquery-descendants-and-children/但是它沒有工作:( – cchiera

回答

1

我相信你問的路線是:

$("nav").children().click(function() { 

你可以這樣做:

$("nav").children().children().click(function() { 

得到 「大孩子」。但是,我可能會使用一個類來使這個更有彈性。

$("nav").find(".navLink").click(function() { 

哪裏navLink是您添加到每個要成爲可點擊的元素的類。

+0

對不起, ()「。(child)()。click(function(){ – cchiera

+0

有沒有辦法做到這一點:$(」nav> (需要)「)。children()。click(function(){kind of thing? – cchiera

+0

是的,這也應該可以工作,如果你可以發佈一個工作示例,人們可能會更好一些。你剛纔發佈的那個應該工作得很好,除非有其他錯誤。當我不能運行代碼時很難說。 –