繁体   English   中英

有没有办法在Javascript中将传递的参数类型提示给IDE(在我的案例中是Webstorm)?

[英]Is there a way to hint the passed argument type to IDE (in my case Webstorm) in Javascript?

我有以下HTML文件,其中包含一些JavaScript

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<div id="root"></div>
<div>
    <label for="new-content">
        <textarea id="new-content" style="width: 400px; height: 100px"></textarea>
    </label>
    <br/>
    <button onclick="addContent(document.getElementById('new-content'))">Submit</button>
</div>


<script>
    function addContent(/*HTMLTextAreaElement*/content) {
        alert(content.value);
    }
</script>
</body>
</html>

我喜欢我可以提示Webstorm(或IntelliJ或Eclipse)是什么类型的content是在function addContent ,但我不喜欢我怎么也不知道它是什么onclick ,这导致以下警告:

在此输入图像描述

这是我的第一个世界问题:我可以在参数中提示document.getElementById('new-content')的类型吗?

我通过反复试验找到了一种方法,以下似乎有效:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<div id="root"></div>
<div>
    <label for="new-content">
        <textarea id="new-content" style="width: 400px; height: 100px"></textarea>
    </label>
    <br/>
    <button onclick="addContent(getNewContent())">Submit</button>
</div>

<div id="content"></div>

<script>
    function getNewContent() {
        return document.getElementById('new-content');/*HTMLTextAreaElement*/
    }

    function addContent(/*HTMLTextAreaElement*/content) {
        document.getElementById('content').innerHTML =
            document.getElementById('content').innerHTML +
            '<p>'.concat(content.value).concat('</p>');
    }
</script>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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