簡體   English   中英

jQuery獲取下一個元素的圖像ID

[英]Jquery get image id of next element

我有一個圖像列表,我正在嘗試使用Jquery從當前元素中查找下一個圖像ID。但是我似乎無法使jquery工作。

<div class="grid" style="position: relative;">
 <div class="preview">
  <img src="1.jpg" id="175" alt="">
 </div>
</div>
<div class="grid" style="position: relative;">
     <div class="preview">
      <img src="2.jpg" id="176" alt="">
     </div>
    </div>
<div class="grid" style="position: relative;">
     <div class="preview">
      <img src="3.jpg" id="177" alt="">
     </div>
    </div>

的JavaScript

var childId=6;
$( ".grid" ).click(function() {
        var image=$(this).find("img");
        var imageId=image.attr("id")
        show_image(imageId,childId);
    });

function show_image(imageId,childId){
  //need to get next image id in the group for next button , but code below is returning undefined
  var nextImageId=$(".grid .preview #" + imageId).next("img").attr("id");
}

在這種情況下,您不能直接使用.next() ,因為圖像不是彼此siblings

var nextImageId= $("#" + imageId).closest('.grid').next(".grid").find('img').attr("id");

使用它,它將在“ nextImageId”變量中為您提供圖像ID。

$( ".grid" ).click(function() { 
     var nextImageId = $(this).closest('.grid').next(".grid").find('img').attr("id");   
});

使用next()獲取下一個網格選擇器,然后找到img

 $(".grid").click(function () {
    var image = $(this).find("img");
    var imageId = image.attr("id")

   var nextImageId= $(this).next(".grid").find("img").attr("id")  // get next image id
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM