Order of onclick and onselect Javascript events
When a user clicks on a select control to select an o开发者_如何学编程ption, do both the onclick
and onselect
events fire? If so, in what order? Is it browser-dependent?
The select
event does not do what you think it does. It fires when you select text within a textbox or textarea. The events that fire when you click on a select element are:
- mousedown
- focus (if the select element did not already have focus)
- mouseup
- click
When you change the selected value by clicking on an item in the select list, the change
event is fired. In IE, this event is also fired every time you change the highlighted item with the keyboard. In Firefox and Chrome, you have to hit the 'enter' key to trigger change
.
Should be fairly easy to test:
<select onclick="alert('click');" onselect="alert('select');"><option>A</option><option>B</option></select>
It should be:
- mousedown
- mouseup
- click
- select
But I'm not sure if nonstandard browsers (IE) always conform to this. If in doubt, test it with a bunch of event listeners.
精彩评论