欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript初級教程(第五課續(xù))第2/3頁

 更新時間:2007年04月05日 00:00:00   作者:  
    選單是我們所學的表單選項中最奇特的一種。有兩種基本的格式:下列選單和列表選單。以下為例子:

    下拉選單:
   

    列表選單:
   

    奇特之處在于這個選單有名稱,但其中的各個選項沒有名稱。例如,在HTML中,第1個選單如下:

  <select name="pulldown_1" size="1">
    <option>probiscus </option>
    <option>spider </option>
    <option>lemur </option>
    <option>chimp </option>
    <option>gorilla </option>
    <option>orangutan </option>
  </select>

    注意這個選單的名稱是pulldown_1,但各個選項沒有名稱。所以要調(diào)用其中的各個選項則有點困難。

    幸好數(shù)組可以幫助我們調(diào)用其中的選項。如果你想改變該下列選單中的第2個選項,你可以這樣做:

    window.document.form_1.pulldown_1.options[1].text = 'new_text';

    這是因為選單的元素有一個選項屬性,而該屬性是選單所有選項的集合而成的數(shù)組。點擊change the selectt然后從下拉選單從下列選單中查看其選項是否已經(jīng)被改變?,F(xiàn)在第2個選項應(yīng)該是*thau*。

    除了選項屬性,選單還有一項屬性叫做selectedIndex。一個選項被選擇后,selectedIndex屬性將變成被選項的數(shù)組索引號(序列號)。選擇第2個列表選單中的一個選項,然后檢查索引號。記住數(shù)組中的第1個選項的索引號是0。

    <a href="#" onClick="alert('index is: ' + window.document.form_1.list_1.selectedIndex);return false;">check the index.</a>

    表單的名稱是form_1,列表選單的名稱是list_1。selectedIndex屬性值為window.document.form_1.list_1.selectedIndex。你還可
以將selectedIndex設(shè)置如下:

    window.document.form_1.list_1.selectedIndex = 1;

    并高亮度顯示第2個選項。

    一旦你得到被選項的索引號,你就可以發(fā)現(xiàn)其內(nèi)容:

    var the_select = window.document.form_1.list_1;

    var the_index = the_select.selectedIndex;

    var the_selected = the_select.options[the_index].text;

    selectedIndex屬性很有用,但如果有多個選項同時被選中,會如何呢?

    選單元素的處理器為onChange()。當選單發(fā)生變化時,則該處理器被激活。

相關(guān)文章

最新評論