提问者:小点点

为什么。onclick在此表单中不起作用,尽管Google Inspect没有显示错误?


因此,我正在为我的Javascript课程(我的大多数问题都将涉及到该课程)进行分配,并使用事件处理程序。 所以我使用了一个。onclick,这样当有人单击表单时,就会显示一些东西。 但当我在浏览器中运行它时,它什么也不做。 HTML或Javascript中没有错别字,因此我将非常感谢您的帮助。

HTML

<html>
<head>
<title>5d</title>
</head>

<body>
<form>
Write something here:
<input type="text" id="textField"> 
<span id="formatReminder"></span>
</form>
<script type="text/javascript" src="5d_javascript.js">
</script>
</body>
</html>

JavaScript

function onclickform() {
document.write("You found me!")
}

document.getElementById("formatReminder").onlick=onclickform;

共1个答案

匿名用户

它不会运行,因为您从未用onclick调用过函数。 应该是这样的:HTML

<html>
<head>
<title>5d</title>
</head>

<body>
<form>
Write something here:
<input type="text" id="textField"> 
<span id="formatReminder"></span>
</form>
<script type="text/javascript" src="5d_javascript.js">
</script>
</body>
</html>

JS

function onclickform() {
document.write("You found me!")
}

document.getElementById("formatReminder").onlick=onclickform();

null

function onclickform() {
document.write("You found me!");
}

document.getElementById("formatReminder").onclick = () => {
  onclickform();
}
<html>
<head>
<title>5d</title>
</head>

<body>
<form>
Write something here:
<input type="text" id="textField"> 
<span id="formatReminder">Click me</span>
</form>
<script type="text/javascript" src="5d_javascript.js">
</script>
</body>
</html>