简体   繁体   English

动态创建新元素

[英]Create new elements dynamically

I am trying to create new elements with name and time.我正在尝试创建带有名称和时间的新元素。 I want to let the user customize them(ex. can be able to change color).我想让用户自定义它们(例如可以改变颜色)。

The dropdown option is only working for the first class it changes the color but the rest of them are not working.下拉选项仅适用于它改变颜色的第一类,但其余的都不起作用。 How can I fix it?我该如何解决?

 function f_color() { var x = document.getElementById('Tcolor1').value; window.alert(x); if (x == 0) { document.getElementById('input1').style.color = "black"; document.getElementById('input2').style.color = "black"; document.getElementById('input3').style.color = "black"; } if (document.getElementById('Tcolor').value == 1) { document.getElementById('input1').style.color = "red"; document.getElementById('input2').style.color = "red"; document.getElementById('input3').style.color = "red"; } if (document.getElementById('Tcolor').value == 2) { document.getElementById('input1').style.color = "blue"; document.getElementById('input2').style.color = "blue"; document.getElementById('input3').style.color = "blue"; } }
 <button id="btn2">Add Another Class</button> <form> <select class="form-control" id="Tcolor" name="Tcolor" style="color:black; font-size: 20px; " onchange="f_color()"> <option value=0>black </option> <option value=1>red</option> <option value=2>blue </option> </select> </form> <form> <input id="input1" name="className"><br> <input id="input2" name="classTime"><br> <input id="input3" name="classNote"><br> </form>

 <button id="btn2">Add Another Class</button> <form> <select class="form-control" id="Tcolor" name = "Tcolor" style="color:black; font-size: 20px; " onchange="f_color()" > <option value=0 >black </option> <option value=1 >red</option> <option value=2 >blue </option> </select > </form> <form> <input id="input1" name="className" ><br> <input id="input2" name="classTime" ><br> <input id="input3" name="classNote" ><br> </form> <script> function f_color(){ var x=document.getElementById('Tcolor1').value; window.alert(x); if ( x == 0) { document.getElementById('input1').style.color = "black"; document.getElementById('input2').style.color = "black"; document.getElementById('input3').style.color = "black"; } if (document.getElementById('Tcolor').value == 1 ) { document.getElementById('input1').style.color = "red"; document.getElementById('input2').style.color = "red"; document.getElementById('input3').style.color = "red"; } if (document.getElementById('Tcolor').value == 2) { document.getElementById('input1').style.color = "blue"; document.getElementById('input2').style.color = "blue"; document.getElementById('input3').style.color = "blue"; } } </script>

remove comment删除评论

In JavaScript, <!-- --> is not a valid comment tag.在 JavaScript 中, <!-- -->不是有效的注释标签。 Also, in f_color , you are using the variable x only once.此外,在f_color ,您只使用了一次变量 x。 You should replace your other comparisons with x as well.您也应该用x替换其他比较。 Prefer CSS classes over the style attribute.更喜欢 CSS 类而不是 style 属性。

Applying all these changes (and a few other miscellaneous changes):应用所有这些更改(以及其他一些杂项更改):

 // do it in javascript document.querySelector(".form-control").onchange = f_color; function f_color(e) { // e has the calling element in it let /*var can be used too*/ x = parseInt(e.target.value, 10); let input1 = document.getElementById("input1"); let input2 = document.getElementById("input2"); let input3 = document.getElementById("input3"); alert(x); let color = ""; if (x === 0) { color = "black"; } else if (x === 1) { color = "red"; } else if (x === 2) { color = "blue"; } alert(color); input1.style.color = color; input2.style.color = color; input3.style.color = color; }
 .form-control { color: black; font-size: 20px; }
 <button id="btn2">Add Another Class</button> <ol> <form> <select class="form-control" name="Tcolor"> <option value="0">black</option> <option value="1">red</option> <option value="2">blue</option> </select> </form> <form> <input id="input1" name="className"><br> <input id="input2" name="classTime"><br> <input id="input3" name="classNote"><br> </form> </ol>


But really, the problem is the mis-spelling of "Tcolor" in但实际上,问题在于“Tcolor”的拼写错误

var x = document.getElementById('Tcolor1').value;

It should be "Tcolor".-应该是“Tcolor”。-

 function f_color() { var x = document.getElementById('Tcolor1').value; window.alert(x); if (x == 0) { document.getElementById('input1').style.color = "black"; document.getElementById('input2').style.color = "black"; document.getElementById('input3').style.color = "black"; } if (document.getElementById('Tcolor').value == 1) { document.getElementById('input1').style.color = "red"; document.getElementById('input2').style.color = "red"; document.getElementById('input3').style.color = "red"; } if (document.getElementById('Tcolor').value == 2) { document.getElementById('input1').style.color = "blue"; document.getElementById('input2').style.color = "blue"; document.getElementById('input3').style.color = "blue"; } }
 <button id="btn2">Add Another Class</button> <form> <select class="form-control" id="Tcolor" name="Tcolor" style="color:black; font-size: 20px; " onchange="f_color()"> <option value=0>black </option> <option value=1>red</option> <option value=2>blue </option> </select> </form> <form> <input id="input1" name="className"><br> <input id="input2" name="classTime"><br> <input id="input3" name="classNote"><br> </form>

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

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