简体   繁体   English

在jQuery中将具有子级更改为具有GrandChildren

[英]Change Has Children to Has GrandChildren in jQuery

Here is an except of javascript. 这是javascript的例外。

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);
    });

Here is an excerpt of html: 这是html的摘录:

http://pastie.org/2929494 http://pastie.org/2929494

Now here's my question. 现在这是我的问题。 With that js and html it does what it is suppose to. 使用该js和html即可完成其预期的工作。 However when I add a needed div inside the nav as such it breaks: 但是,当我在导航中添加所需的div时,它会中断:

http://pastie.org/2929497 http://pastie.org/2929497

I imagine the problem is in the original js above, there are two lines about ("nav").children and imagine with my new code they are actually now grandchildren or such. 我想象问题出在上面的原始js中,关于(“ nav”)。children有两行,并用我的新代码想象它们实际上是现在的孙子。 So most likely need to make minor adjustments to both of those lines, but not sure to what. 因此,最有可能需要对这两条线进行较小的调整,但不确定要做什么。

Thanks in advance! 提前致谢!

I believe the line you're asking about is : 我相信您要询问的电话是:

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

You could do: 您可以这样做:

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

to get the "grand children". 得到“大孩子”。 However, I would probably use a class instead to make this a bit more resilient. 但是,我可能会改用一个类,以使其更具弹性。

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

Where navLink is a class that you add to each of the elements which you want to be clickable. navLink是一个类,您可以将其添加到要单击的每个元素中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM