为什么按下按钮时我的onclick功能没有激活?

时间:2018-06-29 21:26:44

标签: javascript html

由于我想扩展自己的编程能力,因此我正在从Java转换为Javascript,虽然不知道为什么,但是我很难调整。例如,我什至不确定为什么我无法运行Javascript函数,没关系以我想要的方式实际工作。

我觉得我可能没有将JS正确连接到我的HTML文档,但是我在脚本标签中包含了正确的文件名,并且这两个文件位于同一文件夹中,所以为什么不使用JS函数甚至被叫?

HTML

<!DOCTYPE html>
<html lang="en">
<head>

  <!-- Basic Page Needs
  –––––––––––––––––––––––––––––––––––––––––––––––––– -->
  <meta charset="utf-8">
  <title>To-do Webapp</title>
  <meta name="description" content="">
  <meta name="author" content="">

  <!-- Mobile Specific Metas
  –––––––––––––––––––––––––––––––––––––––––––––––––– -->
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- FONT
  –––––––––––––––––––––––––––––––––––––––––––––––––– -->
  <!--<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">-->

  <!-- CSS
  –––––––––––––––––––––––––––––––––––––––––––––––––– -->
  <link rel="stylesheet" href="css/normalize.css">
  <link rel="stylesheet" href="css/skeleton.css">
  <link rel="stylesheet" href="css/styling.css">
  </head>

  <!-- scripts
  –––––––––––––––––––––––––––––––––––––––––––––––––– -->
<script src="script.js"></script>


<body>

  <div class="container">
    <div class="row">
      <div class="one-half column" style="margin-top: 15%">
        <h2>To-do Webapp</h4>
        <p>Enter your task into the box below.</p>
      </div>
    </div>

    <form>
      <div class="row">
        <div class="ten columns">
          <input class="u-full-width" type="text" id="inputtask" placeholder="Enter a task"/>
        </div>
        <div class="two columns">
          <!-- <input class="button" onclick="addTask()" value="Add task"> -->
          <button onclick="addTask()">Button element</button>
        </div>
      </div>
    </form>
</body>

JS

function addTask() {
    "use strict";
    /*global document: false */
    document.alert("hello");
}

1 个答案:

答案 0 :(得分:6)

它是window.alert("hello");,而不是document.alert("hello");

并且不建议使用内联脚本,请改用addEventListener

堆栈片段

document.addEventListener("DOMContentLoaded", function(event) {

  document.querySelector('button').addEventListener('click', addTask);

});


function addTask() {
  "use strict";
  /*global document: false */
  window.alert("hello");
}
<div class="container">
  <div class="row">
    <div class="one-half column" style="margin-top: 15%">
      <h2>To-do Webapp</h4>
        <p>Enter your task into the box below.</p>
    </div>
  </div>

  <form>
    <div class="row">
      <div class="ten columns">
        <input class="u-full-width" type="text" id="inputtask" placeholder="Enter a task" />
      </div>
      <div class="two columns">
        <!-- <input class="button" onclick="addTask()" value="Add task"> -->
        <button>Button element</button>
      </div>
    </div>
  </form>
</div>


请注意,如上面的代码示例所示,需要在加载DOM之后调用添加事件侦听器的行,如下面的示例所示,也可以在body末尾的脚本中调用。

<div class="container">
  <div class="row">
    <div class="one-half column" style="margin-top: 15%">
      <h2>To-do Webapp</h4>
        <p>Enter your task into the box below.</p>
    </div>
  </div>

  <form>
    <div class="row">
      <div class="ten columns">
        <input class="u-full-width" type="text" id="inputtask" placeholder="Enter a task" />
      </div>
      <div class="two columns">
        <!-- <input class="button" onclick="addTask()" value="Add task"> -->
        <button>Button element</button>
      </div>
    </div>
  </form>
</div>

<script>
  document.querySelector('button').addEventListener('click', addTask);

  function addTask() {
    "use strict";
    /*global document: false */
    window.alert("hello");
  }
</script>

相关问题