function _index(el) {
var index = 0;
if (!el || !el.parentNode) {
return -1;
}
//previousElementSibling属性返回指定元素的前一个兄弟元素(相同节点树层中的前一个元素节点)。
while (el && (el = el.previousElementSibling)) {
//console.log(el);
index++;
}
return index;
}
function _index(el) {
var index = 0; // 声明index初始值为0
if (!el || !el.parentNode) { // 如果el元素不存在或者没有父元素
return -1; // 返回-1
}
//previousElementSibling属性返回指定元素的前一个兄弟元素(相同节点树层中的前一个元素节点)。
// https://developer.mozilla.org/zh-CN/docs/Web/API/Element/previousElementSibling
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/while
while (el && (el = el.previousElementSibling)) { // 如果存在el元素并且el替换为把el的兄弟元素
//console.log(el);
index++; // index=index+1
}
return index; // 返回index
}