为了重申T.J.Crowder在评论中所说的话,这是你对用户界面所做的一个尴尬的改变。也就是说,可以区分点击
select
元素并单击
option
要素
如果select没有
multiple
属性集
. 在Internet Explorer中尝试以下代码:
$(document).ready(function ()
{
$("#sortSelect").click(function (event) {
if (event.offsetY > this.offsetHeight)
$(this.options[this.selectedIndex]).click();
});
$("#sortSelect option").click(function (event)
{
alert('clicked option '+this.parentNode.selectedIndex);
});
});
它之所以起作用,是因为当您单击一个option元素时,click事件会触发select元素。通过检测您已在select元素检查的范围之外单击
event.offsetY > this.offsetHeight
,可以使用jquery触发当前选定选项元素的click事件。