且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法读取未定义的属性'classList'

更新时间:1970-01-01 07:59:24

看起来这可能会发生,因为我的范围是外部函数,因此它总是被设置为最大值我的价值。因此, div [i + 1] 将始终未定义。你能做的是:

It seems like this might occur because i is scoped to the outer function, so it'll always be set to the max value for i. Because of this, div[i + 1] will always be undefined. What you could do is:

div[i].onclick = function(idx) {
  this.classList.remove("active");
  if(idx < div.length - 1) div[idx + 1].classList.add("active");
}.bind(div[i], i);

什么绑定需要快照您传递给函数的变量值,如以及这个的价值。首先是this的值,然后是函数的参数。然后它返回它被调用的函数,其参数的某个子集绑定到您提供的值。任何未绑定的参数都可以由调用者提供,但所有绑定的参数必须在调用者提供的参数之前。

What bind does is it takes a snapshot of the values of variables that you are passing to functions, as well as a value for "this". The value for "this" comes first, then arguments for the function follow it. It then returns the function it was called on, with some subset of its arguments bound to the values you provided. Any arguments that were not bound can be provided by the caller, but all bound arguments must come before arguments supplied by the caller.