[英]How to handle click event in javascript?
Today is my first day at a new job (a Front End Lead on a large website.) One of my tasks is to implement a button that fires an event when it's clicked. 今天是我从事新工作的第一天(大型网站上的前端主管)。我的任务之一是实现一个按钮,当单击该按钮时会触发该事件。 I had no clue, so after googling a bit, I came up with this: 我一无所知,因此经过一番搜寻后,我想到了:
<html>
<head>
<script type="text/javascript">
function popup();
{
alert("Hello World") ==> alert("Hello World");
}
</script>
</head>
<body>
<input type="button" value="Click Me!" onclick="popup()"></input><br></br>
</html>
</body>
The problem is, when I click my button, nothing happens. 问题是,当我单击按钮时,什么也没发生。
EDIT Updated based on MICHEL's comments 编辑根据MICHEL的评论进行了更新
remove the semi-colon: 删除分号:
function popup()
{
alert("Hello World")
}
that should work ^_^ 应该工作的^ _ ^
see it working here: http://jsfiddle.net/maniator/fNeJh/ 看到它在这里工作: http : //jsfiddle.net/maniator/fNeJh/
Your HTML is invalid. 您的HTML无效。 Change 更改
</html>
</body>
to 至
</body>
</html>
Also, the <br>
tag has no content, so you don't need a closing tag at all (for HTML) or you can write it as a self-closing tag: <br/>
. 另外, <br>
标签没有内容,因此您根本不需要关闭标签(对于HTML),也可以将其编写为自动关闭标签: <br/>
。
As far as the JavaScript error goes, Neal's answer is correct. 就JavaScript错误而言,Neal的答案是正确的。
This should work 这应该工作
<html>
<head>
</head>
<body>
<script type="text/javascript">
function popup()
{
alert("Hello World");
}
</script>
<input type="button" value="Click Me!" onclick="popup()"><br/><br/>
</body>
</html>
Remove semi-colon after 之后删除分号
Bad : 不好:
function popup();
Good : 好:
function popup()
You might want to add one after alert("Hello World") ==> alert("Hello World"); 您可能想在alert(“ Hello World”)==> alert(“ Hello World”);之后添加一个。
函数声明后不应使用分号...关于javascript的简单教程可以让您开始使用javascript ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.