Element.classList无法读取属性'toggle'

时间:2017-07-05 22:31:58

标签: javascript string add

我收到了这个错误:

  

未捕获的TypeError:无法读取HTMLTableCellElement.toto中未定义的属性“toggle”

有人可以帮忙吗?

var comments = document.getElementsByClassName('organisation');
var numComments = comments.length;

function toto(e){
    /*this.style.cssText='background-color:white';*/
    comments.classList.toggle('.maClasse');
}

for (var i = 0; i < numComments; i += 1) {
    comments[i].addEventListener('click', toto, false);
}

1 个答案:

答案 0 :(得分:0)

很好!

var comments = document.getElementsByClassName('organisation');
var numComments = comments.length;

//function toto(e){
    //this.style.cssText='background-color:white';
    //comments.classList.toggle('.maClasse')
    function myFunction() {
        this.classList.toggle("maClasse");
    };
//}

for (var i = 0; i < numComments; i += 1) {
    comments[i].addEventListener('click', myFunction, false);
}