ES6在类方法中添加css类

时间:2017-05-19 18:29:14

标签: css3 es6-class

如何在isCompleted方法中将css类添加到此类的实例?我尝试过类似的东西,但它没有用?

class Task {
    constructor(title, description) {
        this._title = title;
        this._description = description;
    }

    addToList () {
        let tasksList = document.getElementById('tasksList');
        let task = document.createElement('li');
        task.setAttribute('class', 'task');

        let taskTitle = document.createElement('h3');
        taskTitle.setAttribute('class', 'task__title');
        taskTitle.innerText = this._title;

        let taskDescription = document.createElement('p');
        taskDescription.setAttribute('class', 'task__description');
        taskDescription.innerText = this._description;

        task.appendChild(taskTitle);
        task.appendChild(taskDescription);
        tasksList.appendChild(task);
    };

    isCompleted(){
         this.classList.add('task-completed');
    }
}

0 个答案:

没有答案