我需要获取一个id,但它位于一个滑块中,我可以访问的唯一点击事件是箭头,但它们无法获取图像的唯一ID。我知道必须有一种方式遍历div,但现在这有点超出了我的联盟。我确实尝试过任何我发现或者来到我身边的东西,但到目前为止都没有成功。
滑块的代码
<div id="myCarousel" class="carousel slide fluid col-md-6 col-md-offset-3" data-ride="carousel" style="height:95%;background-color: #000;">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active" style="border:1px solid #febe27;margin-bottom:125px;"></li>
{% for storyItem in craft.entries.section('stories').find() %}
{% for previewImage in storyItem.slideshowImage %}
<li data-target="#myCarousel" data-slide-to="1" style="border:1px solid #febe27;margin-bottom:125px;"></li>
{% endfor %}
{%endfor%}
</ol>
<div class="carousel-inner" role="listbox" style="height:87%;position:relative;">
<div class="item active">
{% for storyItem in craft.entries.section('stories').limit(1) %}
{% for previewImage in storyItem.slideshowImage %}
<div id="{{storyItem.id}}" class="imgContentWrapper 1" data-id="{{storyItem.id}}"><a href="#"><img src="{{ previewImage.url('slideshowImage') }}" alt="" border="0" /></a>
</div>
{% endfor %}
{%endfor%}
</div>
{% for storyItem in craft.entries.section('stories').offset(1) %}
{% for previewImage in storyItem.slideshowImage %}
<div class="item ">
<div id="{{storyItem.id}}" class="imgContentWrapper" data-id="{{storyItem.id}}"><a href="#"><img src="{{ previewImage.url('slideshowImage') }}" alt="" border="0" /></a>
</div>
</div>
{% endfor %}
{%endfor%}
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true" style="margin-left:-25px;"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true" style="margin-right:-25px;"></span>
<span class="sr-only">Next</span>
</a>
我正试图抓住div中的{{storyItem.id}}(有两个活动项和常规项)在glyphicon上点击&#39;。
非常感谢任何帮助。
答案 0 :(得分:2)
这应该是诀窍......
$('.glyphicon-chevron-left, .glyphicon-chevron-right').on('click', function() {
var id = $('.carousel-inner').find('item.active').find('.imgContentWrapper').data('id');
});
答案 1 :(得分:0)
我认为a.carousel-control
与您的图片位于同一#myCarousel
div中。如果是这样,你可以遍历父母并找到你想要的图像:
$('.carousel-control').on('click', function() {
var imageId = $(this).parent().find('#your_image_selector');
});