为了账号安全,请及时绑定邮箱和手机立即绑定

Button[index - 1]报错未定义。

Button[index - 1]报错未定义。

Dryad安 2017-12-01 19:29:12
        window.onload=function()         {             var Box = document.getElementById("Box");             var Picture = document.getElementById("Picture");             var Button = document.getElementById("Button").getElementsByTagName("span");             var arrow_left = document.getElementById("arrow_1");             var arrow_right = document.getElementById("arrow_2");             var index = 1;             function ShowButton()             {                 Button[index - 1].className = "show";             }             function Scroll(offset)             {                 var NewLeft = parseInt(Picture.style.left) + offset;                 Picture.style.left = NewLeft + "px"                 if(NewLeft > -600)                 {                     Picture.style.left = -1800 + "px";                 }                 if(NewLeft < -1800)                 {                     Picture.style.left = -600 + "px";                 }             }             arrow_left.onclick=function()             {                 index += 1;                 ShowButton();                 Scroll(600);             }             arrow_right.onclick=function()             {                 index -= 1;                 ShowButton();                 Scroll(-600)             }          }         </script>报错:TypeError: Button.NaN is undefined如题,写到圆点跟随图片切换高亮的时候,这行代码报错了;我认为应该是变量index为局域变量,无法在其他封装函数里使用造成的,不知道是不是这样……翻了下课程下面的答案,好像没有和我情况遭遇一样的同学。
查看完整描述

2 回答

已采纳
?
传说中的陈百万

TA贡献12条经验 获得超1个赞

你的index是在onload中定义的,如果不依靠传参,当然不能在内部方法中调用,所以才会给出上面的报错。

查看完整回答
1 反对 回复 2017-12-02
?
qq_Dreamy_旧城_0

TA贡献24条经验 获得超2个赞

请使用 isNaN() 来判断一个值是否是数字。原因是 NaN 与所有值都不相等,包括它自己。我看到有NAN这里应该是类型有问题吧,

查看完整回答
1 反对 回复 2017-12-02
  • 2 回答
  • 0 关注
  • 1597 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信