jQuery-从其子级确定父级索引

| 早上好。 我喝了一些咖啡来弥补Javascript / jQuery问题带来的压力。 基本上与这个问题相反:确定父级中的子级索引 实际的HTML(DOM)
<body>
<div id=\"content\">
    <div class=\"contentbox\">
        <div class=\"content\">
            <h2>Image Gallery Header</h2>
            <div class=\"imageGallery\">
                <div class=\"image\">
                    <a href=\"javascript:void(0);\" class=\"prevImg\"><i class=\"sx028\"></i></a>
                    <ul>
                        <li id=\"g2i1\">
                            <img src=\"imgs/imageGallery1.jpg\" alt=\"\" width=\"505\" height=\"298\" />
                            <p>Image Caption...</p>
                        </li>
                        <li id=\"g2i2\">
                            <img src=\"imgs/imageGallery2.jpg\" alt=\"\" width=\"505\" height=\"298\" />
                            <p>Image Caption...</p>
                        </li>
                    </ul>
                    <a href=\"javascript:void(0);\" class=\"nextImg\" title=\"nächstes Bild\"><i class=\"sx029\"></i></a>
                </div>
                <div class=\"thumbs\">
                    <a href=\"javascript:void(0);\" class=\"prevImg\">&nbsp;</a>
                    <ul>
                        <li>
                            <img src=\"imgs/imageGallery1.jpg\" alt=\"\" width=\"149\" height=\"88\" />
                        </li>
                        <li>
                            <img src=\"imgs/imageGallery2.jpg\" alt=\"\" width=\"149\" height=\"88\" />
                        </li>
                    </ul>            
                    <a href=\"javascript:void(0);\" class=\"nextImg\">&nbsp;</a>
                </div>
            </div>
            <h2>Image Gallery Caption</h2>
            <p>
                Some text.
            </p>
        </div>

        <div class=\"content\">
            <h2>Image Gallery Header</h2>
            <div class=\"imageGallery\">
                <div class=\"image\">
                    <a href=\"javascript:void(0);\" class=\"prevImg\"><i class=\"sx028\"></i></a>
                    <ul>
                        <li id=\"g2i1\">
                            <img src=\"imgs/imageGallery4.jpg\" alt=\"\" width=\"505\" height=\"298\" />
                            <p>Image Caption...</p>
                        </li>
                        <li id=\"g2i2\">
                            <img src=\"imgs/imageGallery5.jpg\" alt=\"\" width=\"505\" height=\"298\" />
                            <p>Image Caption...</p>
                        </li>
                    </ul>
                    <a href=\"javascript:void(0);\" class=\"nextImg\"><i class=\"sx029\"></i></a>
                </div>
                <div class=\"thumbs\">
                    <a href=\"javascript:void(0);\" class=\"prevImg\">&nbsp;</a>
                    <ul>
                        <li>
                            <img src=\"imgs/imageGallery4.jpg\" alt=\"\" width=\"149\" height=\"88\" />
                        </li>
                        <li>
                            <img src=\"imgs/imageGallery5.jpg\" alt=\"\" width=\"149\" height=\"88\" />
                        </li>
                    </ul>            
                    <a href=\"javascript:void(0);\" class=\"nextImg\">&nbsp;</a>
                </div>
            </div>
            <h2>Image Gallery Caption</h2>
            <p>
                Some text.
            </p>
        </div>
    </div>
</div>
</body>
感谢您阅读那堆大书。 伪JQuery
$(\".nextImg\").click(function() { 
    var activeGallery = $(this).parents(\".imageGallery\").index();
    alert(\"You\'ve clicked the next image button of image Gallery #\" + activeGallery);
});
结果(警告消息) 您单击了图像库#1的下一个图像按钮<-如果您通过index()单击\“。imageGallery \”的\“。nextImg \”按钮;共1 您单击了图像库2的下一个图像按钮<-如果您单击带有index()的\ .imageGallery \的\“。nextImg \”按钮;共2 题: 如何“爬升” class = \“ nextImage \”的父级到元素div class = \“ imageGallery \”并响应div class \“ imageGallery \”的索引? 这对于我的画廊项目的最后一步非常重要。感谢您的任何回应!     
已邀请:
就像是
$(\".nextImg\").click(function() { 
    var $galleries=$(\'.imageGallery\');
    var activeGallery = $galleries.index($(this).closest(\".imageGallery\"));
    alert(\"You\'ve clicked the next image button of image Gallery #\" + activeGallery);
});
activeGallery
会告诉您所有
.imageGallery
中当前
.imageGallery
(包括单击的链接的索引)的索引位置。这是从零开始的,因此您可能希望为人类可读性+1。     

bab

这不是您要的,但是我认为这正是您所需要的:
$(\".nextImg\").click(function() { 
    var $activeGallery = $(this).closest(\".imageGallery\");
    // In here, you call methods on $activeGallery
});
http://api.jquery.com/closest/     
var activeGallery = $(this).closest(\".content\").index();
我想也许这就是您要追求的目标,您必须再上一层,
.content
是div,该div在整个doud的范围内编制了索引,从那里您可以定位到其子画廊     

要回复问题请先登录注册