vs.code.value无法被智能感知识别 [英] Vs code .value is not recognized by intellisense

查看:162
本文介绍了vs.code.value无法被智能感知识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对javascript很陌生,但是在工作时,我试图找出一种方法来获取用户将输入到html输入文本框中的文本,但是由于某种原因vs代码无法识别所需的intellisense去做这个. [我的html代码:

I am very new to javascript, but as I was working I was trying to figure out a way to get the text a user would input into an html input text box, but for some reason vs code does not recognize the intellisense required to do this. [my html code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="text" id="first-input">
    <button id="submit-button">Submit</button>
    <script src="main.js" type="text/javascript"></script>
</body>
</html>

] 1

[

let firstInput = document.getElementById('first-input');
let submitButton = document.getElementById('submit-button');

submitButton.onclick = function () {
    console.log(firstInput.value);
}

] 2

为了在输入文本元素中获取数据,该代码为.value,但是,当我开始输入".val-"时,唯一的VS代码推荐是.nodeValue.

In order to get the data inside an input text element, the code for that is .value, however when i start typing ".val-" the only recommendation VS code has is for .nodeValue.

没有.value选项,只有.nodeValue

这意味着,为了得到我想要的,我使用的是vs代码的intellisense无法识别的命令.但是,即使intellisense找不到它,代码仍然可以按预期运行.

This means, in order to get what I want, I am using a command that the intellisense for vs code doesn't recognize. However, even though intellisense does not find it, the code still operates as intended.

代码可用

是否需要安装或执行某些操作才能访问此信息?我担心还有许多其他事情无法识别,这会使学习这种语言变得更加困难.预先感谢

Is there something I need to install or do to get access to this information? I am concerned there are many other things it doesn't recognize and will make learning this language more difficult. Thanks in advance

推荐答案

这是因为getElementById()不一定返回具有.value属性的元素.例如,它可以返回<div>元素.

This is because getElementById() doesn't necessarily return an element with a .value property. For example, it could return a <div> element.

为了使智能感知知道您将拥有一个.value属性,您必须使用JSDoc注释指示变量firstInput将是一个<input>元素.像这样:

In order for the intellisense to know that you will have a .value property, you have to indicate with a JSDoc comment that the variable firstInput is going to be an <input> element. Like this:

/**
 * @type HTMLInputElement
 */
let firstInput = document.getElementById('first-input');
/**
 * @type HTMLButtonElement
 */
let submitButton = document.getElementById('submit-button');

submitButton.onclick = function () {
  console.log(firstInput.value);
}

不要让您感到困惑,但是VSCode使用TypeScript增强其智能感知能力,

Not to confuse you, but VSCode uses TypeScript to power its intellisense, even for JavaScript code.

这篇关于vs.code.value无法被智能感知识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆