简体   繁体   English

我的“点击” addEventListener效果不佳

[英]my “click” addEventListener does not work well

I'm using the following code to add listeners to a (that I append to a TBODY in the table) so i can see the nice highligh effect when I put my mouse over the TR. 我正在使用以下代码将侦听器添加到(我将其追加到表中的TBODY上),以便将鼠标放在TR上时可以看到不错的highligh效果。 I also have a "click" event, that makes the whole row "clickable" so user can click easily on the whole row and go to that particular page. 我还有一个“单击”事件,该事件使整个行“可单击”,因此用户可以轻松单击整个行并转到该特定页面。

I'm using JS, because I use AJAX call to populate the TR's (and their few TD's). 我正在使用JS,因为我使用AJAX调用来填充TR(以及它们的少数TD)。

First 12 elements in my list are done nicely with PHP, but after that i load next 10, 10, 10...by Ajax call. 使用PHP可以很好地完成列表中的前12个元素,但之后我通过Ajax调用加载下一个10、10、10...。

When I use PHP and add javascript: to the TR with "onClick" it works perfectly, but in this code below (javascript), only the mouseover and mouseout work fine, the "click" event listener adds to all window.location.href's the last value of i, insted of current value (13, 14, 15)...it adds to all only 15 (so always, last value of i - my counter...it does not increment as the counter does). 当我使用PHP并通过“ onClick”将javascript:添加到TR时,它可以正常工作,但是在下面的代码(javascript)中,只有mouseover和mouseout可以正常工作,“ click”事件侦听器会添加到所有window.location.href i的最后一个值,插入当前值(13、14、15)...它加起来只有15(因此,i的最后一个值-我的计数器...它不会像计数器那样增加)。

I think there's something wrong regarding how the event listener functions, how it initialises, something I do not know. 我认为事件侦听器如何起作用,如何初始化以及我不知道的地方有问题。

             for(i=0; i<10; i++){

                    myTr.addEventListener("mouseover",function(){
                        this.style.backgroundColor = "#083636"
                        this.style.cursor = "pointer"
                    });

                    myTr.addEventListener("mouseout",function(){
                        this.style.backgroundColor = "transparent"
                    });

                    myTr.addEventListener("click",function(){
                        window.location.href = '/clubbers/' + clubber_url + '/' + i + '#threads'
                    });

             }

*forgot to start my for bracket when typing/copying the code, now it looks good *在输入/复制代码时忘了在括号中输入我的括号,现在看起来不错

Your problem is that the variable i from the outer for loop isn't captured properly by the event, because by time the event is evaluated the loop already finished, and the variable is at it's last value. 您的问题是,外部for循环中的变量i无法被事件正确捕获,因为在评估事件时,循环已经完成,并且变量处于其最后一个值。

look at this post for some solutions: 查看此帖子以了解一些解决方案:

Caputured variables in Javascript Javascript中的带帽变量

It's a scope issue. 这是一个范围问题。 Basically you're passing i to your eventListener, which is the same variable being incremented by the loop. 基本上,您是将i传递给eventListener,它是由循环递增的同一变量。 See How do JavaScript closures work? 请参阅JavaScript闭包如何工作? for a better explanation 以获得更好的解释

You can just do: 您可以这样做:

myTr.addEventListener("click",(function(j){
  return function(e) {
    window.location.href = '/clubbers/' + clubber_url + '/' + j + '#threads'
  }
})(i));
function clickTr(i){
    return function(){
        window.location.href = '/clubbers/' + clubber_url + '/' + i + '#threads';
    }
}
for(i=0; i<10; i++){

                myTr.addEventListener("mouseover",function(){
                    this.style.backgroundColor = "#083636"
                    this.style.cursor = "pointer"
                });

                myTr.addEventListener("mouseout",function(){
                    this.style.backgroundColor = "transparent"
                });

                myTr.addEventListener("click",clickTr(i));

         }

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

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