IE8 simple alert is failing?

2019-01-29 05:04发布

Why isn't the following piece of code working in IE8?

<select>
    <option onclick="javascript: alert('test');">5</option>

Quite bizarre - no alert is shown in IE8. I do not see the error icon in the left corner as well. Of course it works in FF and Opera. Any ideas?

3条回答
Emotional °昔
2楼-- · 2019-01-29 05:35

Have you tried just:

onclick="alert('test');"

Pretty sure you don't need the javascript: prefix.

查看更多
神经病院院长
3楼-- · 2019-01-29 05:37

All versions of IE (6,7,8) do not support ANY event handlers on the option elements.

This is a (fairly) well known bug that the IE team has indicated they are in no rush to fix. :-(

Then again Opera, Safari & Chrome all have limited or no support for event handlers on options too.

Lack of events on options: bug 280

(related) Lack of styles on options: bug 281

It should be noted that "Edge" (think IE12 on Windows 10) is currently showing that this issue is fixed in preview releases.

查看更多
甜甜的少女心
4楼-- · 2019-01-29 05:41

Putting an onclick handler on an <option> element seems.... weird to me. You might want to switch that to the more common onchange event of the <select>. You can still do whatever you want to do from there, and this is the "accepted" way of doing whatever you want to do to the select. That being said, you might want to try removing the javascript: part of it. That is only needed when you are executing Javascript in a link href for example. An onclick handler expects javascript.

查看更多
登录 后发表回答