源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p>点击按钮获取 select 元素第一个子元素的文本:</p> <select id="mySelect" size="4"> <option>Google</option> <option>Runoob</option> <option>Taobao</option> <option>Wiki</option> </select> <br><br> <button onclick="myFunction()">点我</button> <p><strong>注意:</strong> IE8 及更早版本的浏览器不支持 firstElementChild 属性。</p> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("mySelect").firstElementChild.text; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
运行结果: