2011-11-27 35 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

是的,这也应该可以工作,如果你可以发布一个工作示例,人们可能会更好一些。你刚才发布的那个应该工作得很好,除非有其他错误。当我不能运行代码时很难说。 –