【问题标题】:Why isnt my button displaying value onto the div为什么我的按钮没有在 div 中显示值
【发布时间】:2022-08-18 23:12:28
【问题描述】:

我想要按钮id 为 number1 到展示价值1输入框它的 id 是question 的缩写。我还想知道我的代码是否可以变得更具可读性。

<!DOCTYPE html>
<html lang=\"en\">
<head>
    <meta charset=\"UTF-8\">
    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
    <title>Calucator</title>
    <style>
        body{
            text-align: center;
        }
    </style>
    <script>
        const quest = document.getElementById(\"quest\");
        const data = quest.value;
        const yourElement = document.createElement(\"div\");
      
        function nums(){
        const num1 = document.getElementById(\'number1\').innerText = 1;
        data.textContent = num1;
    }
    function run() {
        nums()
        yourElement.textContent = data
        quest.appendChild(yourElement);
    }
    
       </script>
</head>
<body>
    <h1>Calucator</h1>
    <input type=\"number\" placeholder=\"Enter now\" name=\"\" id=\"quest\">
    <button onclick=\"run()\">=</button>
    <br>
    <button onclick=\"\" id=\"number1\">1</button>
</body>
</html>

    标签: javascript html function onclick


    【解决方案1】:
    <script>
        const quest = document.getElementById("quest");
        const data = quest.value;
        const yourElement = document.createElement("div");
        //PROBLEM 1: You are not attaching yourElement to the DOM. See Element.insertBefore
        function nums(){
          const num1 = document.getElementById('number1').innerText = 1;
          data.textContent = num1;
        }
        function run() {
          nums()
          yourElement.textContent = data
          quest.appendChild(yourElement);
        }
    
       </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多