案例:javascript案例     状态:可编辑再运行    进入竖版
 运行结果 
x
 
1
<html>
2
<head>
3
<script type="text/javascript">
4
function insertBeforeSelected()
5
  {
6
  var x=document.getElementById("mySelect");
7
  if (x.selectedIndex>=0)
8
    {
9
    var y=document.createElement('option');
10
    y.text='Kiwi';
11
    var sel=x.options[x.selectedIndex];  
12
    try
13
      {
14
      x.add(y,sel); // standards compliant
15
      }
16
    catch(ex)
17
      {
18
      x.add(y,x.selectedIndex); // IE only
19
      }
20
    }
21
  }
22
</script>
23
</head>
24
<body>
25
26
<form>
27
<select id="mySelect">
28
  <option>Apple</option>
29
  <option>Pear</option>
30
  <option>Banana</option>
31
  <option>Orange</option>
32
</select>
33
<input type="button" onclick="insertBeforeSelected()" value="Insert option before selected" />
34
</form>
35
36
</body>
37
</html>
38