HTML DOM length 属性

定义和用法

length 属性可返回下拉列表中选项的数目。

语法

  1. selectObject.length=number

实例

下面的例子可返回下拉列表的 id:

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function getLength()
  5. {
  6. alert(document.getElementById("mySelect").length)
  7. }
  8. </script>
  9. </head>
  10. <body>
  11.  
  12. <form>
  13. <select id="mySelect">
  14. <option>Apple</option>
  15. <option>Pear</option>
  16. <option>Banana</option>
  17. <option>Orange</option>
  18. </select>
  19. <input type="button" onclick="getLength()"
  20. value="How many options in the list?">
  21. </form>
  22.  
  23. </body>
  24. </html>