简体   繁体   English

设置或替换标准选择元素

[英]Styling or Replacing the Standard Select Element

I've done a lot of searching and can't seem to find the answer to what I thought might be a fairly straight forward question. 我已经进行了很多搜索,但似乎找不到我认为可能是非常简单的问题的答案。

I want to style a select on my page so that it is basically a green square with the selected number (1 - 20) shown in white in the middle. 我想在页面上设置一个选择的样式,以便它基本上是一个绿色的正方形,中间以白色显示所选的数字(1-20)。

Here is what I have so far: 这是我到目前为止的内容:

select
{
    width: 1.2em;
    height: 1.5em;
    background-color: #3CB23C;
    -moz-border-radius: 0.2em;
    border-radius: 0.2em;
    border: 0.06em outset #666;

    text-align: center;
    color: #fff;
    font-weight: bold;
    font-size: 4em;
}

It looks fine so far but I cannot find anything on how to remove the arrow in order to make this look like a plain box with the number in. 到目前为止看起来还不错,但是我无法找到如何删除箭头的任何方法,以使其看起来像是带有数字的普通框。

(And before I get a thousand replies on why I shouldn't do this... I know . I have read them all before!! I'm not asking you to tell me if I should I'm asking how I do ! It is for a mobile app where a number will be selected by tapping the green box and then choosing the number from the resulting list. It will be obvious what the box does so there is no need to get into an accessibility conversation here please). (还没等我了解了为什么我不应该这样做一千个回复...... 我知道了 。我以前!!我不求你告诉我,如果我我问我怎么阅读它们!它是针对移动应用程序的,通过单击绿色框,然后从结果列表中选择该号码,可以选择一个号码。很明显,该框的作用是,因此无需在此处进行可访问性对话。

I have read a lot about not being able to style selects and other form elements very easily and I have also read a lot of answers that go something like: 我已经读了很多关于不能很容易地设置选择样式和其他表单元素样式的知识,并且还阅读了很多类似以下内容的答案:

"You can't style the control it's self, but using JavaScript you can hide it and create a JavaScript functional control to act like a drop down list. When an item is selected it selects the item in the drop down list." “您无法设置控件本身的样式,但是可以使用JavaScript对其进行隐藏,并创建一个JavaScript功能控件以充当下拉列表。选择一个项目后,它将在下拉列表中选择该项目。”

but then surprisingly few examples. 但是令人惊讶的是,很少有例子。

So does anyone have a way pf styling the arrow so that it 'disappears' or if it is not possible to use simply CSS to remove the arrow then does any one have any good examples of how to replicate this effect with Javascript? 那么,有没有人可以通过某种方式来设置箭头的样式,使其“消失”呢?或者,如果不可能仅使用CSS来删除箭头,那么有没有人有很好的例子来说明如何使用Javascript复制这种效果?

Thank You 谢谢

In wekbit browsers, you can use: 在wekbit浏览器中,您可以使用:

select {
  -webkit-appearance: none;
}

It doesn't work in ie or firefox, even with -moz-appearance: none; 即使在-moz-appearance: none; ,它也无法在ie或Firefox中使用-moz-appearance: none; or appearance: none; appearance: none;

jsFiddle jsFiddle


But IMHO, it is better to do this way: style customized elements and animate them with jQuery. 但是恕我直言,最好采用这种方式:设置自定义元素的样式并使用jQuery对其进行动画处理。 Then add a hidden select that is filled with jQuery with the elements above. 然后添加一个包含上述元素的jQuery的隐藏选择。 It is a bit longer, but this way you won't have so much browsers compatibility problems. 它更长一些,但是这样您就不会有太多的浏览器兼容性问题。

@pinouchon's answer is correct for how to remove the arrow. @pinouchon的答案对于如何删除箭头是正确的。 And if you're OK with the default styling for the part that drops down, you can save yourself a whole ton of work (keyboard usage, accessibility concerns, cross-platform behavior, mousedown-vs.-click-vs.click and hold, ...). 而且,如果您对下拉部分的默认样式还可以,则可以节省大量工作(键盘使用,可访问性问题,跨平台行为,mousedown-vs.-click-vs.click并按住,...)。

But, while you can use -webkit-appearance / -moz-appearance to override the style for the part that is initially displayed, there doesn't seem to be any way to restyle the part that drops down. 但是,尽管您可以使用-webkit-appearance / -moz-appearance覆盖最初显示的零件的样式,但是似乎没有任何方法可以重新设置掉落的零件的样式。 In that case, you'll need to go the route of creating a fake select menu with JavaScript and inserted HTML elements, and hoping you cover all the abovementioned use cases. 在这种情况下,您将需要使用JavaScript和插入的HTML元素创建一个伪造的选择菜单,并希望涵盖所有上述用例。

An example of this latter approach that our team has begun using is Felix Nagel's selectmenu jQuery UI plugin . 我们的团队已开始使用后一种方法的示例是Felix Nagel的selectmenu jQuery UI插件

here is little tip how you might do it. 这里有个小技巧,您可能会如何做。 idea is to hide arroes with owerflow:hidden; 想法是用owerflow隐藏Arroes:隐藏; div. div。

<style>
div{
    float:left;
    width:90px;
    height:100px;
    overflow:hidden;
}
select
{
    float:left;
    width: 3.2em;
    height: 1.5em;
    background-color: #3CB23C;
    margin-left:-8px;

    text-align: center;
    color: #fff;
    font-weight: bold;
    font-size: 4em;
}
</style>
<div>
    <select>
        <optgroup label="Server-side languages">
        <option>21</option>
        <option>22</option>
        </optgroup>
        <optgroup label="Client-side languages">
        <option>23</option>
        <option>25</option>
        </optgroup>
    </select>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM