jquery - 选择选项中的颜色选择器

标签 jquery css html twitter-bootstrap

我有一个颜色选择选项。

<select class="form-control">
    <option value="yellow">Yellow</option>
    <option value="red">Red</option>
    <option value="green">Green</option>
    <option value="blue">Blue</option>
</select>

但我想让它们变成颜色而不是文本。这可能吗?

我找到了 Bootstrap 颜色选择器。但我不希望你选择其他颜色。

最佳答案

  1. 使用值或文本作为选项的背景。
  2. 请注意,悬停的选项将变成蓝色,因为这是该选项的默认功能

$(".form-control option").each(function() {


  $(this).css("background-color", $(this).val())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control">
 <option ></option>
    <option value="yellow">Yellow</option>
    <option value="red">Red</option>
    <option value="green">Green</option>
    <option value="blue">Blue</option>
</select>

关于jquery - 选择选项中的颜色选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45207271/

相关文章:

html - 图像悬停颜色叠加

html - 在包含图像和文本的 html 页面中创建垂直线

jquery - 如何说服浏览器从缓存中加载图像而不是从服务器获取

html - 如何将按钮与文本(标签)放在同一行

jquery - Android WebView Javascript 接口(interface)与浏览器冲突

html - 在 IE11 中使用边框图像(带切片)?

html - 在具有基于 px 边距的容器内使用基于 % 的宽度 div

css - 全屏视频背景 - <video> vs YouTube/Vimeo-&lt;iframe&gt;?

jquery - 如何在提交表单之前等待ajax验证完成?

javascript - 按钮显示和隐藏内容时出现问题