2011-09-01 42 views
2

我有一個頁面,其中有大小爲「4」的< select/>元素。其中一些人被禁用。 IE8的智慧決定不顯示這些列表框的選定項目(該頁面在其他瀏覽器中很好)。我試圖用jQuery給它們着色。它似乎在當地工作。但是當我發佈到我的開發服務器時,它停止工作。這是我使用jQuery:IE 8 - 禁用選擇列表框

$(document).ready(function() { 
     $('select').each(function() { 
      if ($(this).attr('size') != undefined && $(this).attr('size') > 1 && $(this).attr('disabled') == 'disabled') { 
       $(this).find('option[selected=selected]').each(function() { 
        $(this).css('background-color', '#15317E'); 
       }); 
      } 
     }); 
    }); 

我也試過在以下幾頁的建議,但也似乎沒有工作。

參考文獻:

如果您能給我任何建議,這將是非常好的!

謝謝!

+0

呃,你想要整個下拉元素的顏色還是隻想着顏色只有項目,這是禁用? – reporter

+0

其列表框和其中一個項目被選中。如果我在SO鏈接中使用css,則會爲整個選擇框着色。我只想爲禁用列表框中的選定項目着色。謝謝! – k25

+0

我試了幾件事情,我的代碼只在InternetExplorer 6,7,8(9未測試,因爲沒有適用的操作系統可用)工作。 FF3.6.20改變了背景,但沒有改變定義的顏色。 Safari和Chrome向我展示了不同的行爲。順便說一句,當我將我的測試站點上傳到本地服務器時沒有任何問題。 – reporter

回答

1

啊,最後我發現了這個問題。出於某種原因,該服務器實例並沒有認識到這一點的jQuery:

$(this).find('option[selected=selected]').each(function() { 
... 
} 

我不知道爲什麼,但因爲開發機和服務器具有jQuery的相同版本,因此必須檢查了這一點。

因此,當我將它更改爲以下內容時,我的本地服務器和開發服務器都開始突出顯示所選項目的顏色!

$(document).ready(function() { 
     $('select').each(function() { 
      if ($(this).attr('size') != undefined && $(this).attr('size') > 1 && $(this).attr('disabled') == 'disabled') { 
       $(this).find('option').each(function() { 
        if ($(this).attr('selected') == 'selected') { 
         $(this).css('background-color', '#15317E'); 
        } 
       }); 
      } 
     }); 
    }); 

謝謝!

0

jquery的條件 - $(this).attr('disabled')將返回布爾值。

因此,在上述條件語句中的條件,以實現爲:

$(document).ready(function() { 
    $('select').each(function() { 
     if ($(this).attr('size') != undefined && $(this).attr('size') > 1 && $(this).attr('disabled')) { 
      $(this).css('background-color', '#15317E'); 
     } 
    }); 
}); 

同爲選定的項目。

如果所選項目在用戶界面上不可見,IE 8的禁用屬性將不會顯示選定項目。 IE 8也會阻止滾動條。說選擇的項目在大小爲4的選擇框中的第10個位置。因此第10個項目在屏幕上將不可見。對於這種情況,建議將禁用的選擇框的大小從4更改爲0.這將確保只有選定的項目在禁用的選擇框中可見。下面是代碼:

$(document).ready(function() { 
    $('select').each(function() { 
     if ($(this).attr('size') != undefined && $(this).attr('size') > 1 && $(this).attr('disabled')) { 
      $(this).attr('size', 0); 
     } 
    }); 
});