this.property未定义:无法读取未定义的属性'classList'

时间:2016-08-04 08:58:41

标签: javascript class oop this undefined

我正在制作一些东西来可视化照片 目标是在“列表”中选择所需的图片,使其显示在主HTML元素上。但是为了帮助您找到列表中的位置,有一个类将边框放在您选择的元素上。

问题:
使用事件this.block.onclick = function ()执行的函数开始很好,.selected已从初始选定元素中删除,但是当this.block.classList.add('selected');出现时,我收到此错误:

  

media_visu.js:26未捕获的TypeError:无法读取未定义的属性'classList'

我尝试将function放在外面,尝试了classNamesetAttribute,但没有改变:我的this.block似乎是undefined

mediavisu.js:

var mediaVisu = function () {
    'use strict';

    window.console.log('mediaVisu loaded');




    var i,
    visu = document.querySelector("#img"),
    Album = [];

    function Photo(nb, folder) {
        this.block = document.querySelector("#list_img_" + nb);
        this.place = 'url(../src/' + folder + '/' + nb + '.jpg)';
        this.block.onclick = function () {
            for (i = 0; i < Album.length; i += 1) {
                window.console.log(Album[i].block);
                if (Album[i].block.classList.contains('selected')) {
                    Album[i].block.classList.remove('selected');
                }
            }
            visu.style.background = this.place;
            window.console.log(visu.style.background);
            window.console.log(this.place);
            this.block.classList.add('selected');
        };
        Album[Album.length] = this;
    }

    var test_a = new Photo(1, "test"),
    test_b = new Photo(2, "test"),
    test_c = new Photo(3, "test"),
    test_d = new Photo(4, "test"),
    test_e = new Photo(5, "test");

    window.console.log(Album);
    for (i = 0; i < Album.length; i += 1) {
        window.console.log(Album[i]);
    }
};

2 个答案:

答案 0 :(得分:0)

在onclick函数中,this将是被点击的元素

所以你可以简单地使用

this.classList.add('selected');

您可能需要重新考虑使用this.place,因为this不会是您认为的this ..一个常见的解决方案如下

function Photo(nb, folder) {
    this.block = document.querySelector("#list_img_" + nb);
    this.place = 'url(../src/' + folder + '/' + nb + '.jpg)';
    var self = this;
    this.block.onclick = function () {
        for (i = 0; i < Album.length; i += 1) {
            window.console.log(Album[i].block);
            if (Album[i].block.classList.contains('selected')) {
                Album[i].block.classList.remove('selected');
            }
        }
        visu.style.background = self.place;
        window.console.log(visu.style.background);
        window.console.log(self.place);
        this.classList.add('selected');
    };
    Album[Album.length] = this;
}

或者,使用bind

function Photo(nb, folder) {
    this.block = document.querySelector("#list_img_" + nb);
    this.place = 'url(../src/' + folder + '/' + nb + '.jpg)';
    this.block.onclick = function () {
        for (i = 0; i < Album.length; i += 1) {
            window.console.log(Album[i].block);
            if (Album[i].block.classList.contains('selected')) {
                Album[i].block.classList.remove('selected');
            }
        }
        visu.style.background = this.place;
        window.console.log(visu.style.background);
        window.console.log(this.place);
        this.block.classList.add('selected');
    }.bind(this);
    Album[Album.length] = this;
}

注意:现在您回到this.block.classList.add('selected'),因为this现在是您之前所期待的this

答案 1 :(得分:0)

您可以使用'this'(在上一个答案中提及)或使用事件目标访问它:

    this.block.onclick = function (e) {
        for (i = 0; i < Album.length; i += 1) {
            window.console.log(Album[i].block);
            if (Album[i].block.classList.contains('selected')) {
                Album[i].block.classList.remove('selected');
            }
        }
        visu.style.background = this.place;
        window.console.log(visu.style.background);
        window.console.log(this.place);
        e.target.classList.add('selected');
    };