HTML按钮ONCLICK函数在JavaScript中未识别

发布于 2025-01-21 15:08:56 字数 1157 浏览 1 评论 0原文

我有一个链接到脚本的HTML文件。 HTML中定义的按钮具有onclick函数'regrementLabel()',但是脚本中的函数会引发警告:

'rezementlabel'被声明,但其值永远不会读取。

function incrementLabel() {

    var text = document.getElementById("testLabel").textContent;
    document.getElementById("testLabel").innerText = (parseInt(text) + 1);

}
<!DOCTYPE HTML>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="styles.css">
        <script src="js/Main.js"></script>

        <title>Test Web App</title>

    </head>

    <body>
        <button onclick="incrementLabel()">Test</button>
        <label id="testLabel">0</label>
    </body>

</html>

除了在HTML中编写功能外,我找不到任何解决问题的解决方案。任何帮助都将不胜感激。

谢谢!

I have an HTML file linked to a script. A button defined in the HTML has an onclick function 'incrementLabel()' but the function in the script throws the warning:

'incrementLabel' is declared but its value is never read.

function incrementLabel() {

    var text = document.getElementById("testLabel").textContent;
    document.getElementById("testLabel").innerText = (parseInt(text) + 1);

}
<!DOCTYPE HTML>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="styles.css">
        <script src="js/Main.js"></script>

        <title>Test Web App</title>

    </head>

    <body>
        <button onclick="incrementLabel()">Test</button>
        <label id="testLabel">0</label>
    </body>

</html>

I can't find any solutions to the problem other than writing the function in a in HTML. Any help would be much appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2025-01-28 15:08:56
<!DOCTYPE HTML>
<script>
function incrementLabel() {

    var text = document.getElementById("testLabel").textContent;
    document.getElementById("testLabel").innerText = (parseInt(text) + 1);

}
</script>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="styles.css">
        <script src="js/Main.js"></script>

        <title>Test Web App</title>

    </head>

    <body>
        <button onclick="incrementLabel()">Test</button>
        <label id="testLabel">0</label>
    </body>

</html>

这很好。检查您是否正确导入JS脚本。

<!DOCTYPE HTML>
<script>
function incrementLabel() {

    var text = document.getElementById("testLabel").textContent;
    document.getElementById("testLabel").innerText = (parseInt(text) + 1);

}
</script>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="styles.css">
        <script src="js/Main.js"></script>

        <title>Test Web App</title>

    </head>

    <body>
        <button onclick="incrementLabel()">Test</button>
        <label id="testLabel">0</label>
    </body>

</html>

This works perfectly fine. Check if you are importing your JS script correctly.

旧情勿念 2025-01-28 15:08:56
  1. 您可以在关闭标签之前将JavaScript代码放入A。

    html:

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="styles.css">
    <script src="js/Main.js"></script>

    <title>Test Web App</title>

</head>

<body>
    <button onclick="incrementLabel()">Test</button>
    <label id="testLabel">0</label>

<script>
    function incrementLabel() {

    var text = document.getElementById("testLabel").textContent;
    document.getElementById("testLabel").innerText = (parseInt(text) + 1);

    }
</script>
</body>
  1. 一些在线工具,例如 https://playcode.io 不识别您的代码在Vscode中测试并正常工作。

  2. 可能必须使用其他在线工具,或者可以测试是否已使用console.log(“工作正常”)将代码很好地导入到JavaScript文件中。

对不起,我的英语。

  1. You can put the javascript code into a before the closing tag .

    HTML:

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="styles.css">
    <script src="js/Main.js"></script>

    <title>Test Web App</title>

</head>

<body>
    <button onclick="incrementLabel()">Test</button>
    <label id="testLabel">0</label>

<script>
    function incrementLabel() {

    var text = document.getElementById("testLabel").textContent;
    document.getElementById("testLabel").innerText = (parseInt(text) + 1);

    }
</script>
</body>
  1. Some of online tools like https://playcode.io don't recognise your code but I have tested in vscode and works fine.

  2. Maybe have to use other online tool or can test if you have imported the code well with a console.log("Works fine") into your javascript file.

Sorry for my english.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文