【问题标题】:my JavaScript using jQuery is not working [closed]我使用 jQuery 的 JavaScript 不工作 [关闭]
【发布时间】:2017-09-09 21:34:11
【问题描述】:

我有一个小程序html和js程序

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="temp.js"></script>
</head>
<body>
<div id='display'></div>
<input type="button" id="creation" value="Create"/>
</body>     

temp.js

$(document).ready(function() {
  $('#creation').click(function() {
    alert("hi");
  });   
});

谁能帮我找出上述程序中的错误。当我单击创建按钮时,它应该提醒我“嗨”。感谢您的帮助

【问题讨论】:

  • 您似乎没有加载 jQuery。
  • 你能解释一下吗
  • jQuery 不是浏览器内置的。

标签: javascript jquery html


【解决方案1】:

你需要加载 jQuery。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

【讨论】:

    【解决方案2】:

    您的代码中没有包含 jQuery:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="temp.js"></script>
    

    确保在第一次引用 jQuery 之前包含它。

    【讨论】:

      【解决方案3】:

      您需要添加 jQuery,然后加载您的 temp.js 脚本。

      使用 jQuery 的工作示例。

      $(document).ready(function() {
        $('#creation').click(function() {
          alert("hi");
        });   
      });
      <!DOCTYPE html>
      <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <title>JQuery</title>
      </head>
      <body>
      <div id='display'></div>
      <input type="button" id="creation" value="Create"/>	
      <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
      
      </body>
      </html>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多