单击按钮时,为什么我的脚本不打印文本

问题描述 投票:0回答:2
<!DOCTYPE html>
<html>
<head>
    <title>WhiteList</title>
</head>
 <body>
  <h1>Hello stranger lets see if you are on the list!!</h1>
  <input id="1" value="type here" placeholder="type here">
  <button onclick="click()">Check</button>
  <br>
  <p id=stuff></p>
  <script type="text/javascript">
    function click(){
      var name = document.getElementById('1').value;
      if (name == "tijmen"){
        document.getElementById("stuff").innerHTML = "<p>hey you are on the list welcome</p>"
      } else{
        document.getElementById("stuff").innerHTML = "<p>sorry you are not on the list</p>"
      }


    } 
  </script>
 </body>
</html>

所以这是代码,没有错误。但问题是,当我插入我的名字并单击按钮时,文本将不会打印....我真的似乎无法找到问题。

javascript html
2个回答
0
投票

尝试添加onclick到JavaScript:

<body>
  <h1>Hello stranger lets see if you are on the list!!</h1>
  <input id="1" value="type here" placeholder="type here">
  <button id="btn">Check</button>
  <br>
  <p id=stuff></p>
  <script type="text/javascript">
   document.getElementById("btn").onclick = function() {
      var name = document.getElementById('1').value;
      if (name === "tijmen"){
        document.getElementById("stuff").innerHTML = "<p>hey you are on the list welcome</p>"
      } else {
        document.getElementById("stuff").innerHTML = "<p>sorry you are not on the list</p>"
      }
    } 
  </script>
</body>

0
投票

这里的问题是click被定义为单击操作,因此引擎认为您正在调用单击按钮。简单测试显示浏览器看到的内容。

<button onclick="console.log(click)">Check</button>

你能做什么?更改名称,甚至更好,使用addEventListener绑定事件侦听器。

© www.soinside.com 2019 - 2024. All rights reserved.