会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 128778个问题
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 137楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 138楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 140楼
WEB前端全系列/第二阶段:JavaScript编程模块/函数与对象 143楼


轮播图.rar

老师你好,我明明做了处理为什么越界还是报错

WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 145楼

老师你好,我在进行边界值控制时好像没有生效,麻烦帮着看下问题在什么地方



var listLi = document.querySelector(".swiplerul").querySelectorAll("li")

var swiperBottomlist = document.querySelector(".swiper-bottom").querySelectorAll("li")

var right = document.getElementById("right")

var left = document.getElementById("left")

console.log(listLi.length);


// 控制图片的显示顺序

var num = 0


left.onclick = function(){

   

    num++

    console.log(num);

    imgDisplay()

   

   

}


right.onclick = function(){

   

    num--

    console.log(num);

    imgDisplay()

   

}



   

// 控制页面显示的方法

function imgDisplay(){

    // 边界值出来

    if(num > listLi.length-1){

        num = 0

    }

    if(num < 0){

        num = listLi.length-1

    }

    // 移除属性

    for(var i = 0;i<listLi.length;i++){

        listLi[i].removeAttribute("class")

        listLi[i].removeAttribute("style")

    }

    // 添加属性

    listLi[num].setAttribute("class","current")

    listLi[num].style.display="block"





    for(var i = 0;i<listLi.length;i++){

        swiperBottomlist[i].removeAttribute("class")

        swiperBottomlist[i].removeAttribute("style")

    }

    swiperBottomlist[num].setAttribute("class","current")

}

for(var i= 0;i<swiperBottomlist.length;i++){

    swiperBottomlist[i].idxx=i

    swiperBottomlist[i].onclick=function(){

        num=this.idexx

        imgDisplay()

    }

}

// 有问题

打印如下:

image.png

超过边界值的值也打印出来了,轮播图中只有4张图

WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 147楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 148楼

百战程序员微信公众号

百战程序员微信小程序

©2014-2024 百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637