简体   繁体   English

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

[英]why is my script not printing text when i click the button

<!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>

so this is the code, there are no errors. 所以这是代码,没有错误。 but the problem is that the text won't print when i insert my name and click the button.... i realy cant seem to find the problem. 但是问题是当我插入我的名字并单击按钮时,文本将不会打印。。。我确实似乎找不到问题。

Problem here is click is defined as a click action so the engine is thinking you are calling the click of the button. 这里的问题是点击被定义为点击动作,因此引擎认为您正在调用按钮的点击。 Simple test shows what the browser sees. 简单测试即可显示浏览器看到的内容。

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

What can you do? 你能做什么? Change the name, or even better, use addEventListener to bind the event listener. 更改名称,或者甚至更好,使用addEventListener绑定事件侦听器。

Try add onclick to the JavaScript: 尝试将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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM