分享

IE8/IE7/IE6下Select列表中disabled的使用

 风中的眼睛_ 2013-05-14
昨天项目中使用select列表list-to-list,其中右边有一项是不可以让用户选中,就是必须选,我使用了selectedFields.options[0].disabled = 'disabled';或者selectedFields.options[0].disabled = true;在IE8下面是正常的,不可选的,但在IE6和IE7下没有起到作用,试了几种方法都不行,最后在网上找到下面代码,只要copy到你的js文件里,然后使用上面的disabled就可以做到不可选的效果了。 
有用到可以看看: 
Javascript代码  收藏代码
  1. /**************************************************************** 
  2. * Author:   Alistair Lattimore 
  3. * Website:  http://www.lattimore./ 
  4. * Contact:  http://www.lattimore./contact/ 
  5. *           Errors, suggestions or comments 
  6. * Date:     30 June 2005 
  7. * Version:  1.0 
  8. * Purpose:  Emulate the disabled attributte for the <option>  
  9. *           element in Internet Explorer. 
  10. * Use:      You are free to use this script in non-commercial 
  11. *           applications. You are however required to leave 
  12. *           this comment at the top of this file. 
  13. * 
  14. *           I'd love an email if you find a use for it on your  
  15. *           site, though not required. 
  16. ****************************************************************/  
  17.   
  18. window.onload = function() {  
  19.     if (document.getElementsByTagName) {  
  20.         var s = document.getElementsByTagName("select");  
  21.   
  22.         if (s.length > 0) {  
  23.             window.select_current = new Array();  
  24.   
  25.             for (var i=0, select; select = s[i]; i++) {  
  26. if(select.id == "your select id") {[//你的select id  
  27. select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }  
  28.                 select.onchange = function(){ restore(this); }  
  29.                 emulate(select);  
  30.             [color=red]}[/color]}  
  31.         }  
  32.     }  
  33. }  
  34.   
  35. function restore(e) {  
  36.     if (e.options[e.selectedIndex].disabled) {  
  37.         e.selectedIndex = window.select_current[e.id];  
  38.     }  
  39. }  
  40.   
  41. function emulate(e) {  
  42.     for (var i=0, option; option = e.options[i]; i++) {  
  43.         if (option.disabled) {  
  44.             option.style.color = "graytext";  
  45.         }  
  46.         else {  
  47.             option.style.color = "menutext";  
  48.         }  
  49.     }  
  50. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约