【问题标题】:$('*').index(currentElement) will give a unique number?$('*').index(currentElement) 会给出一个唯一的数字吗?
【发布时间】:2010-10-19 15:22:09
【问题描述】:

$('*').index(currentElement) 会给出一个唯一的数字吗?
我问是因为我无法理解JQuery docs 中的索引方法好

【问题讨论】:

    标签: javascript jquery html dom


    【解决方案1】:

    是的,它将返回您可以在 jQuery 集合中找到元素的索引 - 例如

    var allElements = $("*");
    var index = allElements.index(someElement);
    
    if(allEmenets[index] == someElement){
      alert("Found it!");
    }
    

    【讨论】:

      【解决方案2】:

      index 方法将搜索当前元素的索引。我可以用代码更好地解释它:

       <div id='div0'></div>
       <div id='div1'></div>
       <span id='span0'></span>
       <div id='div2'></div>  
      
       $('div').index( $('#div0')[0] ); //index is 0 as it is the first of all divs
       $('*').index( $('#div0')[0] ); //index is 0 as it is the first of all elements
       $('span').index( $('#span0')[0] ); //index is 0 as it is the first of all spans
       $('*').index( $('#span0')[0] ); //index is 2 as it is the third of all elements
      

      【讨论】:

        【解决方案3】:

        “是的”。 (只要索引的上下文始终是整个 DOM,我应该对此进行限定)否则,正如其他答案所述,答案是否定的。

        它将为您提供所有 DOM 元素中元素的索引。如果 DOM 发生变化,它将不再有效。

        需要这样一个索引的原因必须是非常不寻常的,我强烈怀疑有更好的方法来完成你想要完成的事情。

        【讨论】:

          猜你喜欢
          • 2010-12-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-10
          • 2023-03-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多