jQuery-选择列表选项hover

我想在鼠标光标hover时提醒选项。 我用这个代码:

$("select > option").hover(function () { alert($(this).attr("id")); }); 

不幸的是,这既不适用于IE,也不适用于FF。

有人可以给我一个提示吗?

你可以试试这个。

 $("select").hover(function (e) { var $target = $(e.target); if($target.is('option')){ alert($target.attr("id"));//Will alert id if it has id attribute alert($target.text());//Will alert the text of the option alert($target.val());//Will alert the value of the option } }); 

如果您通过添加“size = x”属性来创建“列表框”类型选择框,如下所示:

  

hover事件将适用于选项元素:

 $('option').hover(function(){ //code here }); 

这是一种解决方法(我觉得相当不错) –

  • 使用mouseover事件将选择框的大小设置为等于no。 它的孩子们
  • 使用mouseenter事件获取选项。 当size属性存在时, mouseenter可以完美地选择选项(我们现在都知道)
  • 使用mouseout事件将选择框的大小设置回1 ,以获得正常的选择框:)

的jsfiddle