HTML/Javascript 按钮不返回警报

HTML/Javascript button doesn't return alert

本文关键字:返回 按钮 Javascript HTML      更新时间:2023-09-26

我用一个按钮做了一个HTML页面,它显示一条消息:"这是一条消息"。一切看起来都很好,但是当我按下按钮时,没有收到消息。我检查了是否有任何错误,但我找不到任何东西。我做错了什么?

<!DOCTYPE html>
<html>
<head>
    <title>Testing JavaScript</title>
    <link rel="stylesheet" type="css" href="Button.css">
</head>
<body>
    <h1>Javascript</h1>
        <p>I am testing JS on this page.<br/>
        This is a button.</p>
    <script>
        var message = "This is a message";
        function popUp()
        {
            alert(message);
        }
    </script>
    <button type="button" onclick="press()">Press me!</button>
</body>
</html>

你应该打电话给onclick="popUp()"

而不是onclick="press()"

onclick="popUp()"

您没有声明名为"press()"的函数,请更改代码

<!DOCTYPE html>
<html>
<head>
    <title>Testing JavaScript</title>
    <link rel="stylesheet" type="css" href="Button.css">
</head>
<body>
    <h1>Javascript</h1>
        <p>I am testing JS on this page.<br/>
        This is a button.</p>
    <script>
        var message = "This is a message";
        function popUp()
        {
            alert(message);
        }
    </script>
    <button type="button" onclick="popUp()">Press me!</button>
</body>
</html>