首先声明一下, 这个方法最初我是从同事阿汤那里学来的. 我只是又做了一些修改,使它更完善.
众所周知,在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>
分享到:
相关推荐
<p>one</p> <div><p>two</p></div> <p>three</p> jQuery 代码: $("div > p"); 结果: [ <p>two</p> ] -------------------------------------------------------------------------------- 在文档的第一个表单...
ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9"> 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)"> title换行 obj.title = ...
<option value=”1″ xss=removed>1</option> <option value=”2″ xss=removed>2</option> </select> 示例:122,点击option,通过srcElement获取事件源目标在IE中是select,Firefox/Safari/Chrome/Opera则...
Sortable is a <s>minimalist</s> JavaScript library for reorderable drag-and-drop lists. Demo: http://rubaxa.github.io/Sortable/ ## Features * Supports touch devices and [modern]...
点击select里面的option,将其赋值到上面的input,直接用jQuery写的,问题是在用IE8打开的时候,点击option没有任何反应
select-option onclick aaaaa bbbbb ccccc [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]运行测试下即可。
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 ...
1: Select 'Tools' --> '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.) ...
- 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 ...
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 ...
- <Save Log> 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 *...
Berlin, Germany, contact: <arno.garrels@gmx.de> 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 ...
* @param click: function 单击事件处理方法 * @param data: object 附加数据 * * value 方法,获取菜单树选中菜单项的 data 数据 * @param all: boolean true 返回所有选中菜单项,false 只返回叶节点菜单...
4. In the IE browser, enter /localhost/<vir-dir>/admin/loginControlTest11.aspx to see the text button version of the working Login control. 5. Enter /localhost/<vir-dir>/admin/loginControlTestL11....
"></div> [removed] // 初始化图表 var myChart = echarts.init(document.getElementById('main')); // JSON文件(地图数据)路径 var uploadedDataURL = "json/522200.json"; // 显示加载动画效果,可以...
根据文档,保证可以在45题中答对百分之七十。实验通过人数过百。 一下为其中部分题目: ...44. What should you use to click in a hidden IE browser? 45. Which recording profile generates full selectors?
* 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 ...
<For more details see the online help> 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...
<param name="Base64" value="..some content.."> =========================================== Key known unresolved issues: - ChartEditorPanel not supported in MS VC++ applications due to incorrect ...
- 0000791: UniDBGrid, UniStringGrid: Option to disable custom renderer to speed-up render time. 0.83.4.819 +---------------------------------------------------------------------------------------- -...