Onclick in select not working in IE(Onclick 选择在 IE 中不起作用)
问题描述
我对 javascript 有点陌生.这个问题可能听起来有点太傻了,但我无法弄清楚为什么以下内容在 IE 中不起作用而在 Firefox 中起作用..
Am a bit new to javascript. This question might sound a bit too silly, but I'm not able to figure it out why the following doesn't work in IE and works in firefox..
<select multiple="multiple">
<option value="tx" onClick="alert('tx');">Texas</option>
<option value="ak" onClick="alert('ak');">Alaska</option>
<option value="ca" onClick="alert('ca');">California</option>
<option value="ws" onClick="alert('ws');">Washington</option>
<option value="tn" onClick="alert('tn');">Tennessee</option>
</select>
在 IE 中没有出现警报(我使用的是 IE8).但它可以在Firefox中使用!!!!!!
The alert doesn't come up in IE (I'm using IE8). But it works in firefox!!!!!
推荐答案
根据 w3schools,选项标签确实支持 onclick 属性.我试过用桶底IE6,但似乎不是这样.
According to w3schools, the option tag does support an onclick attribute. I tried with with bottom of the barrel IE6 and this doesn't seem to be the case.
最简单的方法是:
<select multiple="multiple" onchange="alert(this.value);">
<option value="tx">Texas</option>
<option value="ak">Alaska</option>
<option value="ca">California</option>
<option value="ws">Washington</option>
<option value="tn">Tennessee</option>
</select>
这并不完全是您所追求的,但应该非常接近.
This is not exactly what you are after, but should be pretty close.
编辑
这需要更多的工作:
<select multiple="multiple" onchange="
switch (this.value){
case 'tx': funcOne(); break;
case 'ak': funcTwo(); break;
etc...
}
">
<option value="tx">Texas</option>
<option value="ak">Alaska</option>
<option value="ca">California</option>
<option value="ws">Washington</option>
<option value="tn">Tennessee</option>
</select>
此时最好将 onchange 封装到 js 文件中的函数中,而不是将其嵌入到 html 中.
At this point it would be appropriate to wrap the onchange into a function in a js file instead of embedding it in the html.
这篇关于Onclick 选择在 IE 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Onclick 选择在 IE 中不起作用


基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01