简体   繁体   English

单击轮播glyphicon时如何获取数据ID

[英]How to get data-id when carousel glyphicon is clicked

I need to grab an id, but it is in a slider and the only click event I get access to is the arrows, but they cannot take the unique id of the image. 我需要获取一个ID,但是它在滑块中,并且我可以访问的唯一click事件是箭头,但是它们不能使用图像的唯一ID。 I know there must be a way traverse to the div but that is a little out of my league right now. 我知道必须有一种遍历div的方法,但是现在我的联盟有点偏离了。 I did try anything that I found or that came to me but have been unsuccessful so far. 我确实尝试了所有发现或遇到的尝试,但到目前为止都没有成功。

The code for the slider 滑块的代码

<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>

I am trying to grab the {{ storyItem.id }} in the divs (there is two the active and the regular item) on the glyphicon 'click'. 我正在尝试在glyphicon“点击”上的div(有两个活动项和常规项)中抓取{{storyItem.id}}。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

This should do the trick mate... 这应该可以解决问题...

$('.glyphicon-chevron-left, .glyphicon-chevron-right').on('click', function() {
    var id = $('.carousel-inner').find('item.active').find('.imgContentWrapper').data('id');
});

I suppose that a.carousel-control are in the same #myCarousel div as your images. 我想a.carousel-control与您的图片位于同一#myCarousel div中。 If so you can traverse to the parent and find the image you want like this: 如果是这样,您可以遍历父级并找到所需的图像,如下所示:

$('.carousel-control').on('click', function() {
    var imageId = $(this).parent().find('#your_image_selector');
});

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

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