HTML DOM index 属性

定义和用法

index 属性可返回下拉列表中选项的索引位置。

语法

  1. optionObject.index

实例

本例可返回下拉列表中所选的选项的索引位置:

  1. <<html>
  2. <head>
  3. <script type="text/javascript">
  4. function alertIndex()
  5. {
  6. var x=document.getElementById("mySelect").selectedIndex;
  7. var y=document.getElementsByTagName("option");
  8. alert(y[x].text + " has the index of: " + y[x].index);
  9. }
  10. </script>
  11. </head>
  12. <body>
  13.  
  14. <form>
  15. Select your favorite fruit:
  16. <select id="mySelect">
  17. <option>Apple</option>
  18. <option>Orange</option>
  19. <option>Pineapple</option>
  20. <option>Banana</option>
  21. </select>
  22. <br />
  23. <br />
  24. <input type="button" onclick="alertIndex()"
  25. value="Show index of the chosen fruit">
  26. </form>
  27.  
  28. </body>
  29. </html>