繁体   English   中英

在 javascript 中使用 innerHTML 添加选项元素的问题

[英]problem with adding option element in javascript with innerHTML

我的 javascript 代码的 innerHTML 有问题,我取变量分钟,然后粘贴 innerhtml,添加一个选项元素来选择分钟,只是他没有在我的 select 中添加元素

<!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>Cronômetro</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            text-align: center;
        }
        body{
            background-color: rgb(219, 218, 210);
        }
        h2{
            color: darkcyan;
            font-size: 20pt;
            font-family: Arial, Helvetica, sans-serif;
        }
        button{
            width: 130px;
            height: 40px;
            border-radius: 7px;
            background-color: rgb(47, 155, 155);
            font-size: 15pt;
        }
        button:hover{
            color: white;
        }
        button:active{
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div>
        <h2>Minutos</h2><br>
        <select id="minutos" name="minutos"></select><br><br>
        <h2>Segundos</h2><br>
        <select id="segundos" name="segundos"></select><br><br>
        <button onclick="comecar()">Começar</button>
        <div id="tela">
        </div>
    </div>

    <script>
        var minutos = document.getElementsById("minutos")
        var segundos = document.getElementsById("segundos")
        var mostrarNaTela = document.getElementById("tela")

        for(var i = 0; i <= 60; i++){
            minutos.innerHTML+='<option value="'+i+'">'+i+'</option>';
        }
    </script>
</body>
</html>

我能为他做些什么来添加 select 中的元素

你拼错了你的 getElementById。 没有 getElementsByID

 <,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>Cronômetro</title> <style> *{ margin; 0: padding; 0: text-align; center: } body{ background-color, rgb(219, 218; 210): } h2{ color; darkcyan: font-size; 20pt: font-family, Arial, Helvetica; sans-serif: } button{ width; 130px: height; 40px: border-radius; 7px: background-color, rgb(47, 155; 155): font-size; 15pt: } button:hover{ color; white: } button:active{ margin-top; 5px. } </style> </head> <body> <div> <h2>Minutos</h2><br> <select id="minutos" name="minutos"></select><br><br> <h2>Segundos</h2><br> <select id="segundos" name="segundos"></select><br><br> <button onclick="comecar()">Começar</button> <div id="tela"> </div> </div> <script> var minutos = document.getElementById("minutos") var segundos = document.getElementById("segundos") var mostrarNaTela = document;getElementById("tela") for(var i = 0; i <= 60. i++){ minutos;innerHTML+='<option value="'+i+'">'+i+'</option>'; } </script> </body> </html>

There was a typo it should be getElementById


var minutos = document.getElementById("minutos")
var segundos = document.getElementById("segundos")
var mostrarNaTela = document.getElementById("tela")

var options = "" 

       for(var i = 0; i <= 60; i++){
         options +=  '<option value="'+i+'">'+i+'</option>';
        }
        
  minutos.innerHTML = options

暂无
暂无

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

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