Javascript 获取倍数的索引<;a>;标签
我需要每个Javascript 获取倍数的索引<;a>;标签,javascript,html,Javascript,Html,我需要每个的索引 JavaScript window.onload = function () { var test = this.test = document.getElementsByTagName('a'); for (var counter = 0; counter < test.length; counter++) { index = Array.prototype.indexOf.call(test, test[counter]);
的索引
JavaScript
window.onload = function () {
var test = this.test = document.getElementsByTagName('a');
for (var counter = 0; counter < test.length; counter++) {
index = Array.prototype.indexOf.call(test, test[counter]);
test[counter].onclick = runTheExample;
console.log(index);
}
}
function runTheExample() {
alert();
}
window.onload=函数(){
var test=this.test=document.getElementsByTagName('a');
用于(变量计数器=0;计数器
我需要在单击元素时发出警报。我认为控制台中的输出是DOM索引号。如果我错了,请纠正我。这是一种方法。您只需使用
Array.prototype.indexOf
查看e.target.parentNode中a
元素的列表,并搜索e.target
的值
e.target
=您的a
元素
e.target.parentNode
=a
父元素
window.addEventListener('DOMContentLoaded',function(){
Array.prototype.forEach.call(document.queryselectoral('a'),函数(link){
link.addEventListener('click',函数(e){
var index=Array.prototype.indexOf.call(e.target.parentNode.queryselectoral('a'),e.target);
警报(索引);
});
});
});代码>
这是一种方法。您只需使用Array.prototype.indexOf
查看e.target.parentNode中a
元素的列表,并搜索e.target
的值
e.target
=您的a
元素
e.target.parentNode
=a
父元素
window.addEventListener('DOMContentLoaded',function(){
Array.prototype.forEach.call(document.queryselectoral('a'),函数(link){
link.addEventListener('click',函数(e){
var index=Array.prototype.indexOf.call(e.target.parentNode.queryselectoral('a'),e.target);
警报(索引);
});
});
});代码>
您可以将节点列表转换为数组,并在forEach
中添加一个单击eventListener
,以获取单击锚的索引
var-anchors=document.getElementsByTagName('a'),
输出=document.querySelector('p'),
anchorsArray=Array.prototype.slice.call(锚);
forEach(函数(元素,索引){
元素addEventListener('click',函数(e){
e、 预防默认值();
output.innerHTML=“您已单击索引为“+索引”的锚点;
},假);
});代码>
您可以将节点列表转换为数组,并在forEach
中添加一个clickeventListener
,以获取单击的锚的索引
var-anchors=document.getElementsByTagName('a'),
输出=document.querySelector('p'),
anchorsArray=Array.prototype.slice.call(锚);
forEach(函数(元素,索引){
元素addEventListener('click',函数(e){
e、 预防默认值();
output.innerHTML=“您已单击索引为“+索引”的锚点;
},假);
});代码>
您可以将索引持久化为html属性,然后在警报方法中使用它。用下面的代码片段更新js文件-
window.onload=function(){
var test=this.test=document.getElementsByTagName('a');
用于(变量计数器=0;计数器 }
您可以将索引持久化为html属性,然后在警报方法中使用它。用下面的代码片段更新js文件-
window.onload=function(){
var test=this.test=document.getElementsByTagName('a');
用于(变量计数器=0;计数器 }
我会尝试下面这样的生活
window.onload = function() {
var test = this.test = document.getElementsByTagName('a');
for (var counter = 0; counter < test.length; counter++){
(function (index) {
test[index].onclick = function () {
runTheExample(index);
}
}(counter));
}
}
function runTheExample(index){
alert(index);
}
window.onload=function(){
var test=this.test=document.getElementsByTagName('a');
用于(变量计数器=0;计数器
我会尝试下面这样的生活
window.onload = function() {
var test = this.test = document.getElementsByTagName('a');
for (var counter = 0; counter < test.length; counter++){
(function (index) {
test[index].onclick = function () {
runTheExample(index);
}
}(counter));
}
}
function runTheExample(index){
alert(index);
}
window.onload=function(){
var test=this.test=document.getElementsByTagName('a');
用于(变量计数器=0;计数器
全选全选您想在父节点列表中获取元素的索引,对吗?您想在父节点列表中获取元素的索引,对吗,是吗?哇,我喜欢这个干净简单的解决方案,你不需要一个函数thnaks@Night5talker,但你不介意往返,是吗?1) 获取索引,2)设置索引(DOM扫描),3)读取索引(DOM扫描)=>对于一个简单的任务来说是一个过度的任务。这不仅仅是一个解决方案,而是一个优雅的解决方案,这不是。@lshettyl告诉我,我对这方面还是新手,最简单的事情就是说它错了
var links = document.querySelectorAll("a");
[].forEach.call(links, function(link, index) { });
link.addEventListener("click", function() {});
link.addEventListener("click", function() {
alert(index);
});
var links = document.querySelectorAll("a");
[].forEach.call(links, function(el, index) {
el.addEventListener("click", function() {
alert(index);
});
});