简体   繁体   English

Twitter Bootstrap 选择箭头丢失

[英]Twitter Bootstrap Select Arrow Missing

I'm having an issue with the select drop down button in twitter bootstrap.我在 Twitter 引导程序中的选择下拉按钮有问题。 It's happening in the two browsers I have installed on the machine (IE11, Chrome) and it's not just restricted to 'my sites'.它发生在我在机器上安装的两个浏览器(IE11、Chrome)中,它不仅限于“我的网站”。

Here is a screenshot of the bootstrap website (OS: Windows 8.1 Broswer: Chrome) ( http://getbootstrap.com/css/#forms-controls ):这是引导程序网站的屏幕截图(操作系统:Windows 8.1 浏览器:Chrome)( http://getbootstrap.com/css/#forms-controls ):

例子

I have checked the console window and all resources are loading correctly.我检查了控制台窗口,所有资源都正确加载。

Could anyone help me with why this is happening / steps to resolve?谁能帮我解释为什么会发生这种情况/解决步骤?

TL;DR: you can't use CSS to change the icon. TL; DR:您无法使用CSS来更改图标。 You'll have to use a library that implements a select-like control using HTML and JavaScript (at the expense of mobile-friendly selects on iOS and Android). 您将不得不使用一个库,该库使用HTML和JavaScript实现类似选择的控件(以iOS和Android上的移动友好选择为代价)。

The icon displayed in <select> is determined by the user's browser or operating system. <select>显示的图标由用户的浏览器或操作系统决定。 You can't change it using CSS. 您无法使用CSS更改它。

Select display in Chrome on a Mac: 选择在Mac上的Chrome中显示:

选择在Mac上的Chrome中显示

Select display in Chrome on a Mac with some styles removed: 选择在Mac上的Chrome中显示,并删除一些样式:

I removed line-height, background-color, border, border-radius, and box-shadow. 我删除了行高,背景颜色,边框,边框半径和框阴影。 Note that the arrow has changed even though I didn't change any related style. 请注意,即使我没有更改任何相关样式,箭头也已更改。

选择在Mac上的Chrome中显示,并删除某些样式

Select display in Chrome on Windows: 选择在Windows上的Chrome中显示:

选择在Windows上的Chrome中显示

Notice that the icons are different, even though the code is the same. 请注意,即使代码相同,图标也不同。

Now what? 怎么办?

Although select isn'g very styleable, there are many libraries that provide a very customizable implementation of a select-like control. 虽然select不是很有风格的,但是有很多库提供了类似select的控件的非常可定制的实现。 I like to use Bootstrap-select . 我喜欢使用Bootstrap-select

This library creates a <div class="caret"></div> that can be styled to change the icon. 该库创建了一个<div class="caret"></div> ,可以设置样式以更改图标。 For example after including the Bootstrap-select JavaScript, the following code: 例如,在包含Bootstrap-select JavaScript之后,以下代码:

HTML HTML

<select class="selectpicker">
    <option>Mustard</option>
    <option>Ketchup</option>
    <option>Relish</option>
</select>

CSS CSS

.caret{
    color: red;
}

Gives me this display: 给我这个展示:

Bootstrap-select示例图像

You'll lose mobile display, though: 但是你会丢失移动显示器:

Using a custom library will disable the mobile-friendly way iOS and Android implement selects, so make sure a custom icon is important enough to you before proceeding. 使用自定义库将禁用iOS和Android工具选择的移动设备友好方式,因此在继续操作之前,请确保自定义图标对您来说足够重要。

在此输入图像描述

Use 使用 下拉图片 for select 选择

select {
-moz-appearance: none;
background: rgba(0, 0, 0, 0) url("../images/dropdown.png") no-repeat scroll 100% center / 20px 13px !important;
border: 1px solid #ccc;
overflow: hidden;
padding: 6px 20px 6px 6px !important;
width: auto;
}

You can't style the <select> element itself at this moment. 此时您无法设置<select>元素的样式。 Every browser applies its own styling to most form elements. 每个浏览器都将自己的样式应用于大多数表单元素。

So you can create your own custom select by hiding the original one, create markup, eg div with ul + li and live it up with javascript. 因此,您可以通过隐藏原始的自定义选择来创建自己的自定义选择,创建标记,例如div with ul + li并使用javascript实现它。

OR 要么

If you don't mind using jQuery, try these libraries: 如果您不介意使用jQuery,请尝试以下库:

I have experienced that behavior with IE on Windows 8.1. 我在Windows 8.1上遇到过IE的这种行为。 For some reason IE renders the arrow differently as soon as you start to style the select element (which bootstrap themes usually do). 出于某种原因,只要您开始设置select元素的样式(通常会执行引导主题),IE就会以不同的方式呈现箭头。 Even something as simple as setting the background color triggers this behavior. 即使是设置背景颜色这样简单的事情也会触发此行为。

The only solution I've found so far is to style the arrow as needed. 到目前为止,我发现的唯一解决方案是根据需要设置箭头样式。 You can use the ::-ms-expand pseudo element for that. 您可以使用:: - ms-expand伪元素。 The following css rule should restore the "default" look: 以下css规则应该恢复“默认”外观:

select::-ms-expand {
  background-color: #fff;
  border: none;
}

I found a solution to this, add this CSS and put 'form-override' class on each select dropdown:我找到了一个解决方案,添加这个 CSS 并在每个选择下拉列表中放置“form-override”类:

.form-override {
  appearance: auto !important;
}

I'm not sure why this works or why it's needed, just wanted to share how I was able to fix this problem.我不确定这为什么有效或为什么需要它,只是想分享我是如何解决这个问题的。 For me it seems to be sporadic, sometimes the problem occurs and I need this style setting to fix it, and sometimes it does not need this fix.对我来说它似乎是零星的,有时会出现问题,我需要这个样式设置来修复它,有时它不需要这个修复。

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

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