JQuery - 从子级确定父级索引
早上好。 我多喝了一些咖啡来补偿这个 Javascript/jQuery 问题带来的压力。
基本上它与这个问题相反: 确定其父项中的子索引
< strong>实际的 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 的下一个图像按钮 <-- 如果您单击“ “.imageGallery”的“.nextImg”按钮,带有index(); 1
您已单击图像库 #2 的下一个图像按钮 <-- 如果您使用 index() 单击“.imageGallery”的“.nextImg”按钮; 2
问题:
我如何“爬”到 class="nextImage" 的父元素到 div class="imageGallery" 和响应 div 类“imageGallery”的索引?
这对于我的画廊项目的最后一步非常重要。感谢您的任何回复!
Good morning.
I drink some more coffee to compensate the stress with this Javascript/jQuery problem.
basically its the opposite thing of this question: Determining child index in it's parent
The actual 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>
Thank you for reading that big pile.
Pseudo JQuery
$(".nextImg").click(function() {
var activeGallery = $(this).parents(".imageGallery").index();
alert("You've clicked the next image button of image Gallery #" + activeGallery);
});
Result (alert message)
You've clicked the next image button of image Gallery #1 <-- if you clicked the ".nextImg" button of the ".imageGallery" with the index(); of 1
You've clicked the next image button of image Gallery #2 <-- if you clicked the ".nextImg" button of the ".imageGallery" with the index(); of 2
Question:
How do I "climb" up the parents of class="nextImage" to an element div class="imageGallery" and response the index of the div class"imageGallery" ?
Its really important for the last step of my gallery project. Thank you for any response!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像
activeGallery
这样的东西会告诉您当前
.imageGallery
(包含被单击的链接的那个)在所有.imageGallery
中的索引位置>s。这是从零开始的,因此您可能需要 +1 以提高人类可读性。Something like
activeGallery
will tell you the index position of the current.imageGallery
(the one that includes the link that was clicked) among all the.imageGallery
s. This is zero-based, so you might want to +1 for human readability.这不是您所要求的,但我认为这正是您所需要的:
http://api.jquery.com /最近/
This is not what you asked for, but I think it's exactly what you need:
http://api.jquery.com/closest/
我想也许这就是你想要的,你必须上升到另一个级别,
.content
是在整个文档范围内索引的div,从那里你可以定位它的子画廊I think maybe this is what you're after, you have to go up another level,
.content
is the div which is indexed in scope of the whole doument, from there you can target it's child gallery