`

让IE的<option>支持click事件

阅读更多
首先声明一下, 这个方法最初我是从同事阿汤那里学来的. 我只是又做了一些修改,使它更完善.

众所周知,在IE里, select的option是不支持onclick事件的, 而在FF 和 OPERA 里, option 是支持onclick事件的.
(safari似乎也不支持,不过暂时我还不知道如何解决safari的问题.)

虽然onchange在某些时刻可以代替 option的click事件, 但是两者并无法做到完全等价.
因为onchange只有在 你点击的option和之前的option不同时才会触发.
当select当前选中的是第一项  而你再次点击第一项时, select是不会被触发的.


下面的代码演示了一种间接实现 option onclick的方法

注意:此方案只适用于 下拉方式的单选select.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>select-option onclick </title>
<script type="text/javascript" >

function simOptionClick4IE(){
	var evt=window.event  ;
	var selectObj=evt?evt.srcElement:null;
	// IE Only
	if (evt && selectObj &&  evt.offsetY && evt.button!=2
		&& (evt.offsetY > selectObj.offsetHeight || evt.offsetY<0 ) ) {
			
			// 记录原先的选中项
			var oldIdx = selectObj.selectedIndex;

			setTimeout(function(){
				var option=selectObj.options[selectObj.selectedIndex];
				// 此时可以通过判断 oldIdx 是否等于 selectObj.selectedIndex
				// 来判断用户是不是点击了同一个选项,进而做不同的处理.
				showOptionValue(option)

			}, 60);
	}
}

function showOptionValue(opt,msg){
	var now=new Date();
	var dt= (1900+now.getYear())+'-'+(now.getMonth()+1)+'-'+now.getDate()+
			' '+now.getHours()+':'+now.getHours()+':'+now.getSeconds()+'.'+now.getMilliseconds();
	var resultZone=document.getElementById('reslut');
	resultZone.style.margin="10px";
	resultZone.innerHTML=dt +" 时,点击了: " + (opt.text + ' = '+opt.value);
}

</script>
 </head>

 <body>

  <select  onclick="simOptionClick4IE()" > 
	<!-- 下面的 onclick="showOptionValue( this )" 是为 ff 和 opera而准备 -->
	<option value="1" onclick="showOptionValue( this )" >aaaaa</option>
	<option value="2" onclick="showOptionValue( this )" >bbbbb</option>
	<option value="3" onclick="showOptionValue( this )" >ccccc</option>
  </select>

<div id="reslut" ></div>
</body>
</html>
8
3
分享到:
评论
4 楼 暂不存在 2015-02-07  
哥们,就差谷歌的内核是好是坏了。。。。
3 楼 qinweittt 2013-11-01  
为什么要用setTimeout呢?
2 楼 fins 2008-06-01  
你说的那是 getFullYear

引用

getYear 方法
请参阅
Date 对象的方法 | getFullYear 方法 | getUTCFullYear 方法 | setFullYear 方法 | setUTCFullYear 方法 | setYear 方法

应用于: Date 对象
要求
版本 1
返回 Date 对象中的年份值。

dateObj.getYear()
必选项 dateObj 参数为 Date 对象。

说明
这个方法已经过时,之所以提供这个方法,是为了保持向后的兼容性。请改用 getFullYear 方法。

对于1900-1999这段时间而言,返回的年份值是一个两位数字的整数,它代表了所保存的年份与 1900 年之间的差距。而对于其它的年份,返回值是一个四位的整数。例如,1996 年的返回值是 96,而 1825 和 2025 年的返回值则相应地为 1825 和 2025。

注意   对于 JScript 1.0 版,getYear 返回的值始终为 Date 对象中的年份与 1900 年之间的差距。例如,1899 年的返回值是 -1, 而 2000 年的返回值是 100。

1 楼 liuying841024 2008-05-31  
js 里的getyear()不需要加1900

相关推荐

    jQuery完全实例.rar

    &lt;p&gt;one&lt;/p&gt; &lt;div&gt;&lt;p&gt;two&lt;/p&gt;&lt;/div&gt; &lt;p&gt;three&lt;/p&gt; jQuery 代码: $("div &gt; p"); 结果: [ &lt;p&gt;two&lt;/p&gt; ] -------------------------------------------------------------------------------- 在文档的第一个表单...

    js使用小技巧

    ENTER键可以让光标移到下一个输入框 &lt;input onkeydown="if(event.keyCode==13)event.keyCode=9"&gt; 文本框的默认值 &lt;input type=text value="123" onfocus="alert(this.defaultValue)"&gt; title换行 obj.title = ...

    javascript各浏览器中option元素的表现差异

    &lt;option value=”1″ xss=removed&gt;1&lt;/option&gt; &lt;option value=”2″ xss=removed&gt;2&lt;/option&gt; &lt;/select&gt; 示例:122,点击option,通过srcElement获取事件源目标在IE中是select,Firefox/Safari/Chrome/Opera则...

    Sortable前端框架

    Sortable is a &lt;s&gt;minimalist&lt;/s&gt; JavaScript library for reorderable drag-and-drop lists. Demo: http://rubaxa.github.io/Sortable/ ## Features * Supports touch devices and [modern]...

    ie8模式下click无反应点击option无反应的解决方法

    点击select里面的option,将其赋值到上面的input,直接用jQuery写的,问题是在用IE8打开的时候,点击option没有任何反应

    javascript option onclick事件ie解决方案 兼容ie,firefox

    select-option onclick aaaaa bbbbb ccccc [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]运行测试下即可。

    swithchy! option

    Change the proxy configuration of Chrome and IE in one click. URL based switch rules. Quickly add rules for currently active website. Quick switch between two proxy profiles. Proxy change ...

    最牛比的css菜单生成工具(收藏)-不下后悔一辈子

    1: Select 'Tools' --&gt; 'Internet Options' from the IE menu. 2: Click the 'Advanced' tab. 3: Check the 2nd option under 'Security' in the tree (Allow active content to run in files on my computer.) ...

    Net Transport 2.96h Build 690

    - Enhanced link click catcher for IE, enabling “Need to be parsed” option would check whether the clicking link is the downloadable resource or not, if yes program would take over it, otherwise let ...

    PICO2000品科监控卡软件

    3. Select the "Properties" option from the popup menu, the click on "Server Type" option; 4. Make sure the "PPP: Internet, Windows NT Server, Windows98 " item is selected from the "Type of Dial-up ...

    ImpREC 1.7c

    - &lt;Save Log&gt; added (Right click on the Log listbox) - Fixed Import Editor to look for the name first before looking for the ordinal - Added 'Skip Main Slot' in Options. It will allow you to trace *...

    ICS delphixe10源码版

    Berlin, Germany, contact: &lt;arno.garrels@gmx.de&gt; ICS is freeware. This software is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any ...

    jQuery+ul+javascript 实现的跨浏览器多级菜单

    * @param click: function 单击事件处理方法 * @param data: object 附加数据 * * value 方法,获取菜单树选中菜单项的 data 数据 * @param all: boolean true 返回所有选中菜单项,false 只返回叶节点菜单...

    Login Control

    4. In the IE browser, enter /localhost/&lt;vir-dir&gt;/admin/loginControlTest11.aspx to see the text button version of the working Login control. 5. Enter /localhost/&lt;vir-dir&gt;/admin/loginControlTestL11....

    echarts.min.js

    "&gt;&lt;/div&gt; [removed] // 初始化图表 var myChart = echarts.init(document.getElementById('main')); // JSON文件(地图数据)路径 var uploadedDataURL = "json/522200.json"; // 显示加载动画效果,可以...

    uipath-Level 1最终测试答案.docx

    根据文档,保证可以在45题中答对百分之七十。实验通过人数过百。 一下为其中部分题目: ...44. What should you use to click in a hidden IE browser? 45. Which recording profile generates full selectors?

    Lightview 2.3.2 Plus!精简优化版插件 for Wordpress

    * Added overlayClose option to disable the overlay close on click for individual views. * Darkened the text and the inner buttons for better visibility on every mouse state. * Better text and image ...

    BURNINTEST--硬件检测工具

    &lt;For more details see the online help&gt; Status ====== This is a shareware program. This means that you need to buy it if you would like to continue using it after the evaluation period. Installation...

    TeeChart Pro Activex Control v5

    &lt;param name="Base64" value="..some content.."&gt; =========================================== Key known unresolved issues: - ChartEditorPanel not supported in MS VC++ applications due to incorrect ...

    unigui0.83.5.820

    - 0000791: UniDBGrid, UniStringGrid: Option to disable custom renderer to speed-up render time. 0.83.4.819 +---------------------------------------------------------------------------------------- -...

Global site tag (gtag.js) - Google Analytics