我有以下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>