控制台在我的JavaScript代码中显示错误

时间:2019-06-27 17:13:51

标签: javascript

我有一些画廊的js代码,但控制台显示以下错误。

function stopMovement() {
    clearInterval(leftInterval);
    clearInterval(rightInterval);
}

leftBtn.addEventListener('mouseenter', moveLeft);
leftBtn.addEventListener('mouseleave', stopMovement);
rightBtn.addEventListener('mouseenter', moveRight);
rightBtn.addEventListener('mouseleave', stopMovement);


//Start this baby up
(function init() {
    var images = [
        'Images/Gallery/1.jpg',
        'http://mymodernmet.com/wp/wp-content/uploads/2017/03/Conrad-Maldives-Rangali-Island.jpeg',
        'Images/Gallery/3.jpg',
        'Images/Gallery/4.jpg'

    ];

错误消息:

Uncaught TypeError: Cannot read property 'addEventListener' of null
    at gallery.js:89
(anonymous) @ gallery.js:89

1 个答案:

答案 0 :(得分:0)

您的leftBtnRightBtn变量未声明或未正确声明,因此该错误表明您正在尝试访问null值上的addEventListener属性