【发布时间】:2012-02-04 19:41:54
【问题描述】:
我想在鼠标光标悬停时提醒一个选项。我使用此代码:
$("select > option").hover(function ()
{
alert($(this).attr("id"));
});
不幸的是,这在 IE 和 FF 中都不起作用。
有人可以给我一个提示吗?
【问题讨论】:
标签: jquery
我想在鼠标光标悬停时提醒一个选项。我使用此代码:
$("select > option").hover(function ()
{
alert($(this).attr("id"));
});
不幸的是,这在 IE 和 FF 中都不起作用。
有人可以给我一个提示吗?
【问题讨论】:
标签: jquery
你可以试试这个。
$("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”属性来制作“列表框”类型的选择框:
<select size="3">
<option>...</option>
<option>...</option>
</select>
悬停事件将作用于选项元素:
$('option').hover(function(){
//code here
});
【讨论】:
这是一个解决方法(我猜相当不错)-
mouseover事件将选择框的大小设置为等于no。它的孩子mouseenter 事件获取选项。当size 属性存在时,mouseenter 可以完美地处理选项(我们现在都知道了)mouseout事件将选择框的大小设置回1,以恢复我们正常的选择框:)【讨论】: