简体   繁体   English

不使用jQuery在ul内获取li索引!

[英]get li index within a ul not with jquery!

I'm using dhtmlgoodies drag-drop-folder-tree and when you click save you get the 我正在使用dhtmlgoodies拖放文件夹树 ,当您单击“保存”时,

id-parentid, id-parentid, etc...

i want to have the li index within it's ul so i can tell their order as well - so i want it to look like this: 我想在它的ul中加入li索引,以便我也能告诉他们订单-所以我希望它看起来像这样:

id-parentid-orderid, id-parentid-orderid, ETC...

their code is not jquery (i know how to do it in jquery with the index() but it is pure JS 他们的代码不是jquery(我知道如何在jquery中使用index()做到这一点,但它是纯JS

how do i get the index of an element within his parent? 我如何获取其父元素的索引?

You'll have to write your own index function ;) 您将必须编写自己的索引函数;)

eg lets say that liNode is an <li> node within an <ul> 例如,假设liNode是<ul><li>节点

function index(array, element) {
   for (var i = 0; i < array.length; i++) {
      if (array[i] == element) return i;
      i++;
   }
   return -1;
}

var allLiNodes = liNode.parentNode.childNodes;
var myIndex = index(allLiNodes, liNode);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM