javascript 在悬停时更改选项背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9651988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:21:11  来源:igfitidea点击:

Change option background color on hover?

javascriptjqueryhover

提问by user198989

How can I change the background color when user hovers it ? I have tried this but it's not working.

用户悬停时如何更改背景颜色?我试过这个,但它不起作用。

<select>
<option>bmw</option>
<option>audi</option>
<option>mercedes</option>
</select>



$('option').hover(function(){
$(this).css("background-color","#FFFFCC");
}); 

回答by David Houde

You cannot use option in this way. The best solution would be styling something similar with <ul>and <li>and interacting with JS.

您不能以这种方式使用选项。最好的解决办法是与造型类似的东西<ul>,并<li>与JS交互。

回答by Bakudan

Since you already use jQuery, there is a pretty nice plugin - image-dropdown+ demo from the makers.

由于您已经使用 jQuery,因此有一个非常不错的插件 - image-dropdown+来自 makers 的演示

回答by shanabus

hoverevent can not be bound to an optiontag. I'll update this post if I find the documentation or proof that says so. In my experience, this cannot be done.

hover事件不能绑定到option标签。如果我找到这样说的文档或证据,我会更新这篇文章。根据我的经验,这是无法做到的。