如何设置<选择>样式元素?

发布于 2024-10-14 16:40:32 字数 247 浏览 2 评论 0原文

如何完全改变

Screeshot

有没有办法用 CSS 做到这一点?如果没有,jQuery?

How can I completely change the design of a <select> element? For example, this is how I want my dropdown to look:

Screeshot

Is there a way to do it with CSS? If not, jQuery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你丑哭了我 2024-10-21 16:40:32

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/

十秒萌定你 2024-10-21 16:40:32

仅使用 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/

抱猫软卧 2024-10-21 16:40:32

有几个 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/

っ左 2024-10-21 16:40:32

您可以尝试以下方法。其中 selectBg.png 是 1px 背景图片。它将给出如下图所示的输出:

modified select element

CSS 代码:

select.onlyOne {
    background: url("./theme/default/images/selectBg.png") repeat scroll 0 0 transparent;
    border: 0 none;
    color: #747862;
    font-size: 12px;
    font-weight: bold;
    height: 40px;
    margin-bottom: 0;
    padding: 8px 4px 8px 0;
    width: 430px;
}

HTML 代码:

<select class="onlyOne" name="somename">
   <option value="somevalue">somevalue</option>
   <option value="somevalue">somevalue</option>
</select>

You can try the following way. Where selectBg.png is 1px background image. It will give an output like following image:

modified select element

CSS Code:

select.onlyOne {
    background: url("./theme/default/images/selectBg.png") repeat scroll 0 0 transparent;
    border: 0 none;
    color: #747862;
    font-size: 12px;
    font-weight: bold;
    height: 40px;
    margin-bottom: 0;
    padding: 8px 4px 8px 0;
    width: 430px;
}

HTML Code:

<select class="onlyOne" name="somename">
   <option value="somevalue">somevalue</option>
   <option value="somevalue">somevalue</option>
</select>
混浊又暗下来 2024-10-21 16:40:32

我们可以使用 jquery 编码来设计 select 标签,请检查下面的编码

(document).ready(function() {
$('#btn-add').click(function(){
    $('#select-from option:selected').each( function() {
            $('#select-to').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
        $(this).remove();
    });
});
$('#btn-remove').click(function(){
    $('#select-to option:selected').each( function() {
        $('#select-from').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
        $(this).remove();
    });
});
$('#btn-up').bind('click', function() {
    $('#select-to option:selected').each( function() {
        var newPos = $('#select-to option').index(this) - 1;
        if (newPos > -1) {
            $('#select-to option').eq(newPos).before("<option value='"+$(this).val()+"' selected='selected'>"+$(this).text()+"</option>");
            $(this).remove();
        }
    });
});
$('#btn-down').bind('click', function() {
    var countOptions = $('#select-to option').size();
    $('#select-to option:selected').each( function() {
        var newPos = $('#select-to option').index(this) + 1;
        if (newPos < countOptions) {
            $('#select-to option').eq(newPos).after("<option value='"+$(this).val()+"' selected='selected'>"+$(this).text()+"</option>");
            $(this).remove();
        }
    });
});

});
html代码是

</select>
<a href="JavaScript:void(0);" id="btn-add">Add »</a>
<a href="JavaScript:void(0);" id="btn-remove">« Remove</a>
<select name="selectto" id="select-to" multiple size="5">
  <option value="5">Item 5</option>
  <option value="6">Item 6</option>
  <option value="7">Item 7</option>
</select>

We can able to design select tag using jquery coding, Check the coding below

(document).ready(function() {
$('#btn-add').click(function(){
    $('#select-from option:selected').each( function() {
            $('#select-to').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
        $(this).remove();
    });
});
$('#btn-remove').click(function(){
    $('#select-to option:selected').each( function() {
        $('#select-from').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
        $(this).remove();
    });
});
$('#btn-up').bind('click', function() {
    $('#select-to option:selected').each( function() {
        var newPos = $('#select-to option').index(this) - 1;
        if (newPos > -1) {
            $('#select-to option').eq(newPos).before("<option value='"+$(this).val()+"' selected='selected'>"+$(this).text()+"</option>");
            $(this).remove();
        }
    });
});
$('#btn-down').bind('click', function() {
    var countOptions = $('#select-to option').size();
    $('#select-to option:selected').each( function() {
        var newPos = $('#select-to option').index(this) + 1;
        if (newPos < countOptions) {
            $('#select-to option').eq(newPos).after("<option value='"+$(this).val()+"' selected='selected'>"+$(this).text()+"</option>");
            $(this).remove();
        }
    });
});

});
and the html code is

</select>
<a href="JavaScript:void(0);" id="btn-add">Add »</a>
<a href="JavaScript:void(0);" id="btn-remove">« Remove</a>
<select name="selectto" id="select-to" multiple size="5">
  <option value="5">Item 5</option>
  <option value="6">Item 6</option>
  <option value="7">Item 7</option>
</select>
忘年祭陌 2024-10-21 16:40:32

这是不可能的!

样式是通过其他html标签和css模拟的!

it's impossible!

the style is simulated by other html tags and css!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文