使用event.currentTarget获取子索引

时间:2016-03-03 14:57:45

标签: javascript dhtml

如何摆脱jQuery" .index()"这里?所有我想要的是一个本地的方式来获得点击按钮的索引,请参阅下面的源代码(完美的工作,但我真的无法找到任何Vanilla-JS解决方案,我不想要使用jQuery完成这么小的任务)。感谢。

<script>
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);
</script>

HTML部分如下所示:

<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>

5 个答案:

答案 0 :(得分:1)

你可以这样做:

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>

答案 1 :(得分:0)

<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>

答案 2 :(得分:0)

希望,它有效:)

无论如何,只针对按钮(即使对于他们我也需要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>

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

ES6风格的答案:

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

const buttonIndex = indexOf(event.currentTarget)