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\"> </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\"> </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\"> </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\"> </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 \”的索引?
这对于我的画廊项目的最后一步非常重要。感谢您的任何回应!
没有找到相关结果
已邀请:
3 个回复
掏得透垦滩
会告诉您所有
中当前
(包括单击的链接的索引)的索引位置。这是从零开始的,因此您可能希望为人类可读性+1。
bab
http://api.jquery.com/closest/
搁手
我想也许这就是您要追求的目标,您必须再上一层,
是div,该div在整个doud的范围内编制了索引,从那里您可以定位到其子画廊