JavaScript 参考手册
HTML DOM Label 对象
您可以使用 document.createElement() 方法来创建 <label> 元素
源代码:
点击运行 »
<form id="myForm" action="form_action.asp"> <input type="radio" name="sex" id="male" value="male"> </form> <p>点击按钮创建 LABEL 元素。</p> <button onclick="myFunction()">尝试一下</button> <script> function myFunction() { var x = document.createElement("LABEL"); var t = document.createTextNode("Male"); x.setAttribute("for", "male"); x.appendChild(t); document.getElementById("myForm").insertBefore(x, document.getElementById("male")); }; </script>
运行结果:
点击运行 »