使用 event.currentTarget 获取 child 的索引

get index of child with event.currentTarget

这里如何去掉jQuery的“.index()”?我正在寻找的只是一种获取单击按钮索引的本机方法,请参阅下面的源代码(完美运行但我真的找不到任何 Vanilla-JS 解决方案,我不想使用 jQuery 对于这么小的任务)。谢谢

    function navButtonClick(ev) {
        var buttonIndex = $(ev.currentTarget).index(); /* How to get rid of jQuery? */

        document.getElementById("output").innerHTML = buttonIndex;
    }

    function startTemplate() {
        var i;
        var navItems = document.querySelectorAll("#navigation button");

        for (i=0; i < navItems.length; i++) {
            navItems[i].addEventListener("click", navButtonClick);
        }
    }

    document.addEventListener("DOMContentLoaded", startTemplate);
    
The HTML part looks like this:

    <nav id="navigation" role="navigation">
        <button type="button"> <em>Navi tab 1</em> </button>
        <button type="button"> Navi tab 2 </button>
        <button type="button"> Navi tab 3 </button>
        <button type="button"> Navi tab 4 </button>
    </nav>

    <div id="output"></div>

<script>
function navButtonClick(index) {
    return function (ev) {
         document.getElementById("output").innerHTML = index;
    }
}

function startTemplate() {
    var i;
    var navItems = document.querySelectorAll("#navigation button");

    for (i=0; i < navItems.length; i++) {
        navItems[i].addEventListener("click", navButtonClick(i));
    }
}

document.addEventListener("DOMContentLoaded", startTemplate);
</script>

你可以这样做:

function navButtonClick(ev) {
    var buttonIndex = Array.prototype.indexOf.call(this.parentElement.children, this);

    document.getElementById("output").innerHTML = buttonIndex;
}

function startTemplate() {
    var i;
    var navItems = document.querySelectorAll("#navigation button");

    for (i=0; i < navItems.length; i++) {
        navItems[i].addEventListener("click", navButtonClick);
    }
}

document.addEventListener("DOMContentLoaded", startTemplate);

检查下面的代码片段:

function navButtonClick(ev) {
  var buttonIndex = Array.prototype.indexOf.call(this.parentElement.children, this);

  document.getElementById("output").innerHTML = buttonIndex;
}

function startTemplate() {
  var i;
  var navItems = document.querySelectorAll("#navigation button");

  for (i = 0; i < navItems.length; i++) {
    navItems[i].addEventListener("click", navButtonClick);
  }
}

document.addEventListener("DOMContentLoaded", startTemplate);
<nav id="navigation" role="navigation">
  <button type="button"> <em>Navi tab 1</em> 
  </button>
  <button type="button">Navi tab 2</button>
  <button type="button">Navi tab 3</button>
  <button type="button">Navi tab 4</button>
</nav>

<div id="output"></div>

希望,成功了:)

无论如何,仅针对按钮(我需要在 chrome 中 closest 甚至它们)。

document.addEventListener('click', function (event) {
  var target = event.target, nodes, i;
  
  if (target.closest) {
    target = target.closest('button');
  }
                                                     
  if (target && target.tagName === 'BUTTON') {
    nodes = target.parentElement.children; 
    for (i=0; nodes[i]!==target; ++i);
    document.querySelector("output").textContent = i;
  }
})
<nav id="navigation" role="navigation">
  <button type="button"> <em>Navi tab 1</em> </button>
  <button type="button"> Navi tab 2 </button>
  <button type="button"> Navi tab 3 </button>
  <button type="button"> Navi tab 4 </button>
</nav>

<output></output>

您可以通过两种不同的方式执行此操作。首先,在添加事件监听器的同时将按钮的索引传递给相应的函数:

function navButtonClick(buttonIndex) {
    document.getElementById("output").innerHTML = buttonIndex;
}

function startTemplate() {
    var i;
    var navItems = document.querySelectorAll("#navigation button");
    for (i = 0; i < navItems.length; i++) {
        navItems[i].addEventListener("click", navButtonClick.bind(null, i));
    }
}

startTemplate();

JSFiddle

其次,创建一个动态检查元素索引的函数:

function navButtonClick() {
    document.getElementById("output").innerHTML = getIndex(this);
}

function getIndex(elm) {
  var parent = elm.parentElement;
  for (var i = 0; i < parent.children.length; i++) {
    if (parent.children[i].isEqualNode(elm)) {
        return i;
    }
  }
}

function startTemplate() {
    var i;
    var navItems = document.querySelectorAll("#navigation button");
    for (i = 0; i < navItems.length; i++) {
        navItems[i].addEventListener("click", navButtonClick);
    }
}

startTemplate();

JSFiddle

ES6 风格的答案:

const indexOf = element => Array.from(element.parentNode.children).indexOf(element)

const buttonIndex = indexOf(event.currentTarget)