本文介绍了Array.prototype.indexOf.call存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我单击一个元素Link1,则该函数中的e.target是节点Link1。我想知道这个节点在ul子节点中的索引是什么,在这种情况下我希望indexOf返回0因为Link1在0位置,而ii点击2我希望它是1.
If i click the a element "Link1" e.target in the function is the node Link1. I want to know in what index this node is in the ul children in this case i want indexOf to return 0 because Link1 is on position 0, and i i click 2 i want it to be 1.
HTML
<div class="link">
<ul>
<li><a>Link1</a></li>
<li><a>Link2</a></li>
</ul>
</div>
JAVASCRIPT
JAVASCRIPT
self.query('.link').forEach(function(linkNode, flikIndex, flikArr) {
dojo.query(linkNode, 'click', function(e) {
var t = e.target; //If i click Link1 this is Link1 and if i click Link2 and so on.
var parent = t.parentNode; //Contains the parent to my a in this case li
var ancestor = t.parentNode.parentNode.childNodes; //Containes 2 li
var index = Array.prototype.indexOf.call(parent, ancestor); //Return -1 but i want it to return 0 because Link1 is on place [0] in the array.
}
}
请帮助我获得正确的索引
Please help me get the right index
推荐答案
var index = Array.prototype.indexOf.call(parent, ancestor);
// ^ Array ^ Element in the array
所以你想要这个:
var index = Array.prototype.indexOf.call(ancestor, parent);
由于 ancestor
是元素(数组 )包含 parent
。
Since ancestor
is the element ("array") that contains parent
.
这篇关于Array.prototype.indexOf.call存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!