简体   繁体   中英

JavaScript Error: document is not defined. [no-undef]

I am new to the Web Development, so i am trying a to learn JavaScript, but i'm always getting this Error.

Code:

var myImage = document.querySelector('img');

myImage.onclick = function () {
    var mySrc = myImage.getAttribute('src');
    if (mySrc === 'images/old-big-garage.png') {
      myImage.setAttribute('src', 'images/name1.png');
    } else {
      myImage.setAttribute('src', 'images/old-big-garage.png');
    }
}

Errors:

JSLint (4)
    4   Missing 'use strict' statement. var mySrc = myImage.getAttribute('src');
    6   Expected 'myImage' at column 9, not column 7.   myImage.setAttribute('src', 'images/papst.png');
    8   Expected 'myImage' at column 9, not column 7.   myImage.setAttribute('src', 'images/old-big-garage.png');
    10  Expected ';' and instead saw '(end)'.   }

ESLint (1)
    1   ERROR: 'document' is not defined. [no-undef]    var myImage = document.querySelector('img');

The problem is with linter: ESlint. Try to install bracketeslint: File ->Extension Manager-> bracketeslint . After you are done with it, Debug -> Reload with Extension ( F5 ) .

You could disable JSlint if you want. This way you will not get irritating warnings.

Hope that will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM