Javascript:Uncaught TypeError:object不是HTMLButtonElement.onclick函数

问题描述 投票:0回答:2

我无法在单击按钮时调用函数。我已经查看了其他问题,并实现了那些但我在标题中出现错误的问题。我究竟做错了什么?

HTML代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>TITLE</title>
    <link rel="stylesheet" href="/static/css/semantic.min.css">
    <script type="text/javascript" src="/static/js/libs/jQuery.min.js"></script>
  </head>
  <body>
    <div class="container">
      <h1>HEADER</h1>
      <div>
        <button class="ui button primary" onclick="submitInput();">
            BUTTON
        </button>
      </div>
    </div>
  </body>
  <script type="text/javascript" src="/static/js/landing_page.js" async></script>
</html>

landing_page.js

$(document).ready(function() {
     window.onload = function(){
        function submitInput() {
            console.log("Submit");
        };
     }
});
javascript html
2个回答
3
投票

landing_page.js

function submitInput() {
  console.log("Submit");
};

在您的代码中,submitInputonload回调中声明,并且在全局范围内不可用。

如果您希望在加载文档后声明submitInput并且仍然可以在全局范围内使用,请执行以下操作:

$(document).ready(function() {
  window.submitInput = function(){
    console.log("Submit");
  }
});

0
投票

页面无法访问您的函数定义。编辑您的.js文件

function submitInput() {
   console.log("Submit");
};

即,删除这些行:

 $(document).ready(function() {
         window.onload = function(){
© www.soinside.com 2019 - 2024. All rights reserved.