编辑下面的代码:【加编码】
<html> <body> <p>点击按钮创建 OPTGROUP 元素。</p> <button onclick="myFunction()">尝试一下</button> <select id="mySelect" size="6"> <option>BMW </option><option>Mercedes </option><option>Ford </option><option>Volvo </option><option>Toyota </option></select> <script> function myFunction() { var x = document.getElementById("mySelect"); var y = document.createElement("OPTGROUP"); y.setAttribute("label", "SwedishCars"); y.appendChild(x.options[3]) x.insertBefore(y,x.options[3]); } </script> </body> </html>
结果: 【此窗口】 帮助?
Try it Yourself - © 自强学堂