实现select下拉框readonly

来源:互联网 发布:工匠精神 知乎 编辑:程序博客网 时间:2024/05/29 10:11

最近在项目里面有一个编辑页面,一部分人可以编辑,一部分人只能查看,所以想实现只能查看的人 不能修改,已办的input和textarea 都有readonly属性,但是select却不行,在国内看了一些帖子的方案是:

 onbeforeactivate="return false" onfocus="this.blur()" onmouseover="this.setCapture()" onmouseout="this.releaseCapture()"
兼容性存在问题,我无意见,在外国技术网站:http://stackoverflow.com上找到了我想要的答案,解决方案很简单,而且也没有兼容性问题:
onfocus="this.defaultIndex=this.selectedIndex;" onchange="this.selectedIndex=this.defaultIndex;"

Tested and working in IE 6, 7 & 8b2,9, Firefox 2 & 3+, Opera 9.62, Safari 3.2.1 for Windows and Google Chrome.

基本上不会存在兼容问题,这里共享出来,让那些还在寻找解决方案的孩纸有个选择.

方案出自:http://stackoverflow.com/questions/368813/readonly-select-tag


发原创帖子不宜,实验方法更不易,还望转载时注明出处!

原创粉丝点击