关于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

你打印下哪里的className有问题

添加:节点.classList.add("类名");
删除:节点.classList.remove("类名");

ol.children[0].className = 'current';// 这样会把所有的class 清除掉
如果对你有帮助的话,麻烦给个采纳啊,非常感谢

1、原因
这里应该是获取点击的小标志,然后给当前点击加class
var index = this.getAttribute('index');

img