有没有办法获得一个对象在其父对象中的 chindren 的 序号
最好不要使用 遍历其父对象的全部子对象的方法(东西多时太慢)
就是
function getChildrenIndex(_obj)
{
  for(var i=0;i<_obj.parentElement.children.length;i++)
  {
     if(_obj.parentElement.children[i]==_obj)
          return i
  }
  return -1
}
_obj.parentElement.children 太多了,而且这个方法使用频率会很高,有没有更好的方法?

解决方案 »

  1.   

    i think it's vevy good
      

  2.   

    function getChildrenIndex(_obj) 

      var childrenCount = _obj.parentElement.children.length;
      for(var i=0;i <childrenCount;i++)   { 
        if(_obj.parentElement.children[i]==_obj) 
              return i 
      } 
      return -1 
      

  3.   

    jqueryp实现
    function getChildrenIndex(_obj) {
         return $(_obj).prevAll().length;
    }
      

  4.   

    jquery实现 
    function getChildrenIndex(_obj) { 
        return $(_obj).prevAll().length;