【问题标题】:Get index of child using jQuery使用jQuery获取孩子的索引
【发布时间】:2013-07-24 14:32:35
【问题描述】:

我想获取被点击的孩子的索引

<div class="parent">
    <div class="child">
        <div class="c1"></div>
        <div class="c2"></div>
    </div>
    <div class="child">
        <div class="c1"></div>
        <div class="c2"></div>
    </div>
    <div class="child">
        <div class="c1"></div>
        <div class="c2"></div>
    </div>
</div>

jQuery:

$('.child .c1').click(function(){
    alert($(this).parent().index())
})

我总是得到-1。我该怎么做?


编辑:

我试过这个:

$('.child .c1').click(function(){
    alert($(this).index())
})

结果始终为 -1。 有什么问题?

【问题讨论】:

  • 您的代码可以正常工作。
  • 这里没有真正的问题jsfiddle.net/Spokey/wpKkm
  • 也适合我。你能做一个演示这个问题的小提琴吗?
  • 取自 jQuery API If the element is not found, .index() will return -1.
  • 只有在this 没有.parent() 时才能找到该元素。如果它被点击,那就意味着它在 DOM 中,并且它确实有一个父节点。

标签: javascript jquery indexing parent-child


【解决方案1】:

var child_index = '';

$('.c1').click(function() {

  var parent = $(this).parent();


  child_index = $(parent).index();

  alert(child_index);

});
.div{
position:absolute;
left:45%;
top:0;
}
.child{
margin:1%;
text-align:center;
background-color:gray;
width:100px;
height:100px;
}

.c1,.c2{
color:white;
background-color:blue;
}
.c2{
color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="div">
  <div class="child">
    <div class="c1">c1</div>
    <div class="c2">c2</div>
    <p>click only work on <strong>c1</strong></p>
  </div>

  <div class="child">
    <div class="c1">c1</div>
    <div class="c2">c2</div>
    <p>click only work on <strong>c1</strong></p>
  </div>

  <div class="child">
    <div class="c1">c1</div>
    <div class="c2">c2</div>
    <p>click only work on <strong>c1</strong></p>
  </div>
</div>
var child_index = ''; //to store .child class parent index when clicked

$('.c1').click(function() {

    var parent = $(this).parent(); //getting the specific parent of c1

    //parent variable value now is = .child
    child_index = $(parent).index();

    alert(child_index);

});

【讨论】:

    【解决方案2】:

    它每次返回 -1 的原因是因为在 Jquery 中 -1 是一个布尔值,表示 false 而 0 是 true。 .index() 基本上是询问一个元素是否存在。

    如果你想找到它是什么数字,这是我认为可以工作的代码。

    var loop = $('.parent .child').length;
    
    $('.child .c1').click(function(){
        for(i=1; i<= loop; i++) {
             if($(this).parent() === $('.parent').children().eq(i)) {
                 alert(i);
             }
        }
    });
    

    【讨论】:

      【解决方案3】:

      编辑由于没有人可以重现该问题,这里有一些可能性:

      • 您使用的 jQuery 版本与您页面上的另一个脚本冲突。因为您的点击事件处理程序似乎正在触发,因为您收到了一个警报框。
        • 尝试使用最新版本的 jQuery。
      • 您的 jQuery 版本与您当前使用的浏览器冲突。当您遇到 JavaScript 问题时,您应该报告您正在使用的库(如果有)和浏览器的版本。
        • 在另一个浏览器中检查您的结果并报告。
      • 在触发点击事件之前,您的 HTML 已被操作。页面上还有哪些其他代码?
        • 分享更完整的源代码清单。

      alert($(this).parent().prevAll().length);
      

      使用 .prevAll 获取所有先前的兄弟,然后使用该集合的长度作为索引。

      edit 如果您希望出于索引目的而忽略其他元素,请使用 .prevAll(".child")。

      编辑您可以尝试一起删除 jQuery

      var elms=document.getElementsByClassName("c1");
      for(var i=0; i<elms.length; i++)
          elms[i].onclick = function() {
              var elm = this;
              var index = 0;
              elm = elm.parentNode;
              if(elm) {
                  while(elm=elm.previousSibling) {
                      if(elm.nodeType == 1) {
                          index++;
                      }
                  }
              } else {
                  alert("There's no parent node");
              }
              alert(index);
          };
      

      【讨论】:

      • 这不是.index() 的本质吗?为什么要重新发明轮子?
      • @Barmar - 我不认为我在这里重新发明任何轮子。只是提供另一个正确(强调正确)的答案。哦,每个人都有自己的。
      • 问题在于,如果 OP 的代码由于在 $(this).parent() 上调用 .index() 而返回 -1,那么您的代码也将返回 -1,所以它并没有真正帮助问题。 ...或者实际上,它会返回 0,即使 .parent() 不匹配任何内容,也会给出误报。
      • 这就是我的评论所说的。起初我说-1 表示不想要的结果,但后来注意到它实际上是0,这会更糟,因为它会让它看起来像是找到了什么。
      • @CrazyTrain - 因为除了 OP 之外没有人得到声称的结果,所以肯定是他使用的标记与他向我们展示的不同,或者他使用的是 jQuery 版本和浏览器的奇异组合。无论如何,鉴于 OP 提供的当前标记和脚本,我的代码 sn-p 将正确运行。我无法读懂人们的想法,所以在 OP 进一步详细说明之前,我会留下我的正确答案。
      猜你喜欢
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2018-08-10
      • 2011-06-11
      • 2015-08-14
      相关资源
      最近更新 更多