渴了可乐 2022-03-31 17:02 采纳率: 100%
浏览 259
已结题

关于js给元素添加类名失效的问题

用js给ol中li加相同的current,结果前面的类名失效了
for (var i = 0; i < ul.children.length; i++) {
    var li = document.createElement('li');
    li.setAttribute('index',i);
    ol.appendChild(li);
    li.addEventListener('click', function () {
        for (var i = 0; i < ul.children.length; i++) {
            ol.children[i].className = '';
        }
        this.className = 'current';
        var index = this.getAttribute('index');
        num = circle = index;
        animat(ul, - index * focusWidth);
    })
}
ol.children[0].className = 'current';
var cloneFirst = ul.children[0].cloneNode(true);
ul.appendChild(cloneFirst);
var num = 0
var circle = 0;
arrow_r.addEventListener('click', function () {
    if (num == ul.children.length - 1) {
        ul.style.left = 0;
        num = 0;
    }
    num++;
    animat(ul, -num * focusWidth);
    circle++;
    if (circle == ol.children.length){
        circle = 0;
    }
    for (var i = 0; i < ol.children.length; i++) {
        ol.children[i].className = '';
    }
    ol.children[i].className = 'current';
})

img

  • 写回答

3条回答 默认 最新

  • 你好!机器人 2022-03-31 17:10
    关注

    你打印下哪里的className有问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 创建了问题 3月31日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改