简体   繁体   English

使用“ for”循环添加“ click”事件监听器

[英]Adding 'click' event listener using 'for' loop

HTML: HTML:

<li>Facebook</li>
<li>Twitter</li>
<li>Souncloud</li>

JS: JS:

var links = ['https://www.facebook.com/', 'https://twitter.com', 'https://soundcloud.com'];

function openURL (url) {
    location.href = url;
}

window.onload = function () {

    var listElement = document.getElementsByTagName('li');

    for (i=0;i<listElement.length;i++) {
        listElement[i].addEventListener('click',openURL(links[i]))
    }

}

I want the code to work in such a way that when the user clicks either of the list elements, it opens up the respective website. 我希望代码以这样一种方式工作:当用户单击任一列表元素时,它将打开相应的网站。 For certain reasons I do NOT want to use the <a> tag. 由于某些原因,我不想使用<a>标记。

The logic to the code is very simple. 代码的逻辑非常简单。 First, a variable is created which returns an array of all the <li> tags in the document. 首先,创建一个变量,该变量返回文档中所有<li>标签的数组。 Then using the 'for' loop, I set an event listener to each of the <li> tags in the array. 然后使用“ for”循环,我为数组中的每个<li>标签设置了一个事件侦听器。 The function I run simply opens the website. 我运行的功能只是打开网站。

Somehow, whenever I open the page, it gets redirected automatically, without clicking, to facebook.com. 无论如何,每当我打开页面时,它都会自动重定向到Facebook.com,而无需单击。 Why does this happen?? 为什么会这样?

Any help would be appreciated, thanks! 任何帮助,将不胜感激,谢谢!

This is because your event handler will be called later (by user action), and that time, i isn't what you want. 这是因为您的事件处理程序将在以后(通过用户操作)调用,而那时, i不是您想要的。 You have to use closure to keep it internal. 您必须使用闭包将其保留在内部。

 var links = ['https://www.facebook.com/', 'https://twitter.com', 'https://soundcloud.com']; function openURL(link) { console.log(link); }; window.onload = function () { var listElement = document.getElementsByTagName('li'); for (i=0;i<listElement.length;i++) { listElement[i].addEventListener('click',(function (i) { return function () { openURL(links[i]); }; }(i))); } } 
 <li>Facebook</li> <li>Twitter</li> <li>Souncloud</li> 

When you do click',openURL(links[i]) , it will call openUrl . 当您click',openURL(links[i]) ,它将调用openUrl You should use .bind(context, params) . 您应该使用.bind(context, params) Also note, first argument of eventListener is event 还要注意,eventListener的第一个参数是event

Also, window.onload = function is considered as bad practice as it will override any previous listener. 同样, window.onload = function被认为是不好的做法,因为它将覆盖以前的所有侦听器。 You should use .addEventListener 您应该使用.addEventListener

 var links = ['https://www.facebook.com/', 'https://twitter.com', 'https://soundcloud.com']; function openURL(event, url) { //location.href = url; console.log(url) } window.addEventListener('load', function() { var listElement = document.getElementsByTagName('li'); for (i = 0; i < listElement.length; i++) { listElement[i].addEventListener('click', openURL.bind(null, event, links[i])) } }) 
 <li>Facebook</li> <li>Twitter</li> <li>Souncloud</li> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM