如何设置<选择>样式元素?选择>
How can I completely change the design of a <select>
element? For example, this is how I want my dropdown to look:
Is there a way to do it with CSS? If not, jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
CSS 只会在某些浏览器中为您提供帮助 - 例如,Internet Explorer 将完全忽略它的 SELECT
,但是,您仍然可以在页面上使用普通的选择而不使用任何 CSS,然后使用优雅降级的 jQuery 插件 (所以当人们没有启用 JS 时,他们仍然会看到原来的选择框)
这样的插件之一就是这个 来自 Filament Group 的出色 jQuery UI 选择菜单,
但是,正如评论中 clonked 所指出的 - 至少使用一些浏览器嗅探来检查人们是否没有从 iPhone 或类似设备访问该菜单,因为
如果您有机会在服务器端使用 PHP,他们可能会在访问 JS 选择框时遇到问题,这里有一个不错的浏览器嗅探库:http://chrisschuld.com/projects/browser-php-detecting -a-users-browser-from-php/
CSS will only help you in certain browsers - for instance, Internet Explorer will ignore it for SELECTs completely
you can, however, still use an ordinary select on page without any CSS whatsoever and then use a jQuery plugin that degrades gracefully (so when people don't have JS enabled, they will still see that original select box)
one of such plugins is this great jQuery UI selectmenu from Filament Group
however, as pointed out by clonked in comments - use at least some browser sniffing to check whether people are not accessing that menu from iPhone or similar devices, as they might have problems accessing that JS select box
if you're by any chance using PHP on the server side, here's a nice browser sniffing library for you: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/
仅使用 CSS 来实现
http://www.filamentgroup.com/lab/jquery_ui_selectmenu_an_aria_accessible_plugin_for_styling_a_html_select/
You will have issues achieving consistent styling of a
<select>
element with CSS alone. Your best bet is to use jquery. Fortunately a very nice plugin has already been created:http://www.filamentgroup.com/lab/jquery_ui_selectmenu_an_aria_accessible_plugin_for_styling_a_html_select/
有几个 jQuery 插件可以帮助您完成此任务。
这是一个轻量级的: http://www.adamcoulombe.info/lab/ jquery/select-box/
这里是一个使用图像从头开始构建一个的教程:
http://www.devirtuoso.com/2009/ 08/styling-drop-down-boxes-with-jquery/
There are several jQuery plugins that will help you accomplish this.
Here is a light-weight one: http://www.adamcoulombe.info/lab/jquery/select-box/
And here is a tutorial to build one from scratch with images:
http://www.devirtuoso.com/2009/08/styling-drop-down-boxes-with-jquery/
您可以尝试以下方法。其中 selectBg.png 是 1px 背景图片。它将给出如下图所示的输出:
CSS 代码:
HTML 代码:
You can try the following way. Where selectBg.png is 1px background image. It will give an output like following image:
CSS Code:
HTML Code:
我们可以使用 jquery 编码来设计 select 标签,请检查下面的编码
});
html代码是
We can able to design select tag using jquery coding, Check the coding below
});
and the html code is
这是不可能的!
样式是通过其他html标签和css模拟的!
it's impossible!
the style is simulated by other html tags and css!