Javascript jquery-获取最近div的id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6452521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:47:46  来源:igfitidea点击:

jquery-Get the id of closest div?

javascriptjquery

提问by maaz

Hi I have a Table with list of vehicle

嗨,我有一张包含车辆清单的表格

 <table>
 <tr class="${(i % 2) == 0 ? 'odd' : 'even'}" id ="rows">
   <td>
      <div class="check_box_div">
       <div class="check_box_list">
         <g:checkBox id = "isActive_${i}" class="isActive" name="isActive"  value="${vehicleInstance?.isActive}" />
          <input type="hidden" value="${vehicleInstance?.id}" class="vehicleId" name="vehicleId" id="isActive_${i}_" />
        </div>
       <div class="display_image" id ="display_image_${i}"></div>
      </div>    
    </td>

  </tr>

table has got many rows I want to get the id of div where the class name is "display_image" for each row i tried to get it like

表有很多行我想获取 div 的 id,其中类名是“ display_image”,对于我尝试获取的每一行

   $(".isActive").click(function() {
     var checkBox_id = $(this).attr("id");
     var checkbox = $('#'+checkBox_id);
     var div_id =$('#'+checkBox_id).closest("div").find(".display_image").attr("id"); // always returns div_id =display_image_0(div id of first row)

This is works for first row but for second row also it returns id of div of first row only what is the change i should make so that i will get the id of div on each row

这适用于第一行,但对于第二行,它也返回第一行的 div id 只返回我应该做的更改,以便我将获得每一行的 div id

回答by Alnitak

Go up until you find the row, and then down again to find the display image:

向上直到找到该行,然后再次向下找到显示图像:

$(".isActive").click(function() {
    var div_id = $(this).closest('tr').find('.display_image').attr(id);
    // ...
});

Note that this doesn't depend on your original element's ID at all - just the layout of your elements within the DOM.

请注意,这完全不依赖于原始元素的 ID - 仅依赖于 DOM 中元素的布局。

More optimal solutions are possible, but this one will work regardless of the relative positions of the .isActiveand .display_imageelements within each row.

更优化的解决方案是可能的,但无论每一行中.isActive.display_image元素的相对位置如何,这个解决方案都将起作用。

回答by James South

Try this.

尝试这个。

 $(".isActive").click(function() {
 var div_id = $(this).parents(".check_box_list").next(".display_image").attr("id");
 }

I used parents rather than parent just in case you shift any of your markup around.

我使用了父母而不是父母,以防万一你改变你的任何标记。

回答by Bj?rn

Use

$(".isActive").click(function() {
    var $row = $(this).closest("tr");
    var $div_id = $row.find("div.display_image").attr("id");
});

jsFiddle: http://jsfiddle.net/5TV9A/8/

jsFiddle:http: //jsfiddle.net/5TV9A/8/

回答by Giuseppe Di Federico

You can store all the info in an Array and handle them after the loop.

您可以将所有信息存储在一个数组中并在循环后处理它们。

$(".isActive").click(function() {
     var checkBox_id = $(this).attr("id");
     var checkbox = $('#'+checkBox_id);
     var div_array = new Array();
     $('.display_image').each(function(index) {
              var div_array[index] = //whatever you want;
      });
     // Now in div_array you have stored all the information you want (the id as well)

Hope it helps

希望能帮助到你

回答by Vivek

closetwill not work here..as much as i know closet works towards parent heirarachy.

closet不会在这里工作......据我所知,壁橱适用于父层次结构。

try this one

试试这个

var div_id =$('#'+checkBox_id).parents(".check_box_div").find(".display_image").attr("id");