简体   繁体   中英

VScode intellisense not working..any suggestions?

When I type the code:

document.queryselector('.classname').

.style and other common suggestions do not appear in the suggested autocomplete menu when I begin to type after the parentheses.

But when I type

document.queryselector('body').

It does?

First of all, you should use querySelector , not queryselector .

Regarding your issue, there is a reason why VScode IntelliSense doesn't show style attribute.

Let's look at this example:

const element = document.querySelector('.classname')
const element2 = document.querySelector('body')

element has a type of Element . And element2 has type HTMLBodyElement which is subclass of HTMLElement .

Element VS HTMLElement

Element is the most general base class from which all element objects (ie objects that represent elements) in a Document inherit. It only has methods and properties common to all kinds of elements. More specific classes inherit from Element . And it doesn't have style attribute.

The HTMLElement interface represents any HTML element. It extends Element and has style attribute.

Then why element2 return HTMLBodyElement ?

VSCode has hardcoded typing when you selecting by exact tag name. You can see that in lib.dom.d.ts

How can fix that?

You can add additional checking and VSCode will understand it:

const element = document.querySelector('.classname')

if (element instanceof HTMLElement) {
    element.style.display = 'none'
}

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