Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 为每个选项选择不同的项目背景色_Html_Css - Fatal编程技术网

Html 为每个选项选择不同的项目背景色

Html 为每个选项选择不同的项目背景色,html,css,Html,Css,我想根据一个特定的类为select中的每个选项设置不同的背景色 <select> <option class="yellow" value="1">Yellow</option> <option class="red" value="2">Red</option> <option class="green" value="3">Green</option> </select> 因此,当

我想根据一个特定的类为select中的每个选项设置不同的背景色

 <select>
  <option class="yellow" value="1">Yellow</option>
  <option class="red" value="2">Red</option>
  <option class="green" value="3">Green</option>
</select>
因此,当我单击选择按钮时,会显示以下内容:

我希望当我打开select时,每个选项项的背景颜色都是类的颜色,如下所示:


你应该使用你的类黄,红,绿,并给他们颜色在你的css

HTML

css

<style>
    .yellow{
        background-color: yellow;
    }    
    .red{
        background-color: red;
    }    
    .green{
        background-color: green;
    }
</style>

你应该使用你的类黄,红,绿,并给他们颜色在你的css

HTML

css

<style>
    .yellow{
        background-color: yellow;
    }    
    .red{
        background-color: red;
    }    
    .green{
        background-color: green;
    }
</style>

这样做很容易,无需上课:

   select option:nth-child(1) {
        background: yellow;
    }
    select option:nth-child(2) {
        background: red;
    }
    select option:nth-child(3) {
        background: green;
    }

并以jQuery为例,自动获取颜色并设置背景

火狐

Internet explorer


这样做很容易,无需上课:

   select option:nth-child(1) {
        background: yellow;
    }
    select option:nth-child(2) {
        background: red;
    }
    select option:nth-child(3) {
        background: green;
    }

并以jQuery为例,自动获取颜色并设置背景

火狐

Internet explorer


我对这个脚本有一个问题,因为当safari在OSX上显示时,它不会对select中的选项进行着色。我最终发现,通过使用Bootstrap,一切都按照我的意图进行:

<!DOCTYPE html>
<html lang="nl">
<head>
    <meta charset="utf-8" />
    <title>font test</title>
    <meta name="generator" content="BBEdit 10.5" />

    <link rel="stylesheet" type="text/css" href="yourpathto/css/bootstrap.min.css" async />
</head>
<body>
    <!-- test color dropdown -->
    <style>
    .hex1{background-color:#eee;}
    .hex2{background-color:red;}
    .hex3{background-color:navy;}
    .hex4{background-color:purple;}
    </style>
    <div id="colordropdown1" class="dropdown" style="width: 300px;">
    <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Kies een kleur
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
    <li role="presentation" class="hex1"><a role="menuitem">Grijs</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation" class="hex2"><a role="menuitem">Rood</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation" class="hex3"><a role="menuitem">Marine</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation" class="hex4"><a role="menuitem">Paars</a></li>
    <li role="presentation" class="divider"></li>
    </ul>
    </div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script type="text/javascript" src="yourpathto/js/jquery-1.11.3.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed --> 
<script type="text/javascript" src="yourpathto/js/bootstrap.js"></script>
</body>
</html>

我对这个脚本有一个问题,因为当safari在OSX上显示时,它不会对select中的选项进行着色。我最终发现,通过使用Bootstrap,一切都按照我的意图进行:

<!DOCTYPE html>
<html lang="nl">
<head>
    <meta charset="utf-8" />
    <title>font test</title>
    <meta name="generator" content="BBEdit 10.5" />

    <link rel="stylesheet" type="text/css" href="yourpathto/css/bootstrap.min.css" async />
</head>
<body>
    <!-- test color dropdown -->
    <style>
    .hex1{background-color:#eee;}
    .hex2{background-color:red;}
    .hex3{background-color:navy;}
    .hex4{background-color:purple;}
    </style>
    <div id="colordropdown1" class="dropdown" style="width: 300px;">
    <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Kies een kleur
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
    <li role="presentation" class="hex1"><a role="menuitem">Grijs</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation" class="hex2"><a role="menuitem">Rood</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation" class="hex3"><a role="menuitem">Marine</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation" class="hex4"><a role="menuitem">Paars</a></li>
    <li role="presentation" class="divider"></li>
    </ul>
    </div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script type="text/javascript" src="yourpathto/js/jquery-1.11.3.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed --> 
<script type="text/javascript" src="yourpathto/js/bootstrap.js"></script>
</body>
</html>

你可以使用ul>li来模拟这个问题。看看这个:这个链接中的解决方案并没有按照我的要求工作。谢谢你在所有情况下,你可以使用ul>li来模拟这个问题。看看这个:这个链接中的解决方案,没有按照我想要/要求的那样工作。非常感谢您使用的是什么web浏览器,您能给我看更多的代码吗?@FrankWinters也许是关于浏览器兼容性的,还有一件事,顺便问一下,您能提供一些JSFIDLE或jsbin吗?谢谢。你用的是什么网络浏览器,你能给我看更多的代码吗?@FrankWinters也许是关于浏览器兼容性的,还有一件事,顺便问一下,你能提供一些JSFIDLE或jsbin吗?谢谢,它坏了!我希望当我打开“选择”时,我会有不同的选项项,每个选项都有一个特定的颜色,就像我在第二张图片中发布的那样。啊,好吧,但是没有Javascript你就做不到。如果你想用Javascript写不出来,我不知道为什么,但我看不到颜色的变化!它似乎不起作用,它是jquery版本,我把它添加到答案上,它不起作用???它不起作用!我希望当我打开“选择”时,我会有不同的选项项,每个选项都有一个特定的颜色,就像我在第二张图片中发布的那样。啊,好吧,但是没有Javascript你就做不到。如果你想用Javascript写不出来,我不知道为什么,但我看不到颜色的变化!它似乎不起作用,它是jquery版本,我把它添加到答案上,它不起作用???