如何更改下拉菜单中的选项字体颜色

huangapple go评论69阅读模式
英文:

How to change the Options Font Color in Dropdown

问题

我正在动态创建下拉选项,我想在下拉选项中为每个选项添加不同的字体颜色。这在Google Chrome上可以工作,但在其他浏览器上不行。
如何解决这个问题
谢谢!!

这是我的代码

<div class="control">
    <label for="c_color_id" id="c_color_id_main_label"> 选择颜色代码 &nbsp;&nbsp; </label>
    <select name='c_color_id[0]' data-form-part="hairforadmin_formula_form" id="c_color_id_main" onchange="ColorCodeCurrent(this);">
        <?php foreach ($colors as $option) {
            if ($option['value'] == '') {?>
                <option value=""> 请从后端添加颜色 </option>
            <?php } else {?>
                <option value="<?php echo $option['value']?>" style="color:<?php echo $block->getColorFontById($option['value']); ?>"><?php echo $option['label']?></option>
            <?php }
        }?>
    </select>
</div>

我尝试在互联网上搜索,但没有找到一个能快速解决我的问题的简单解决方案。

英文:

I am dynamically creating options for dropdown I want to add diffrent font color on each of option in dropdown. This is working on Google chorome but not on ther browsers.
How to solve this problem
Thanks!!

Here is my code

`&lt;div class=&quot;control&quot;&gt;
            &lt;label for=&quot;c_color_id&quot; id=&quot;c_color_id_main_label&quot;&gt; Select Color Code &amp;nbsp;&amp;nbsp; &lt;/label&gt;
            &lt;select name=&#39;c_color_id[0]&#39; data-form-part=&quot;hairforadmin_formula_form&quot; id=&quot;c_color_id_main&quot;
                onchange=&quot;ColorCodeCurrent(this);&quot;&gt;
                &lt;?php foreach ($colors as $option) {
                if ($option[&#39;value&#39;] == &#39;&#39;) {?&gt;
                &lt;option value=&quot;&quot;&gt; Please Add Colors from backend &lt;/option&gt;
                &lt;?php } else {?&gt;
                &lt;option value=&quot;&lt;?php echo $option[&#39;value&#39;] ?&gt;&quot; style=&quot;color:&lt;?php echo $block-&gt;getColorFontById($option[&#39;value&#39;]); ?&gt;&quot;&gt;&lt;?php echo $option[&#39;label&#39;] ?&gt;&lt;/option&gt;
                &lt;?php }
                }?&gt;
            &lt;/select&gt;
&lt;/div&gt;`

I try to search on the internet but didnot find a simple solution that can quickly solve mt problem

答案1

得分: 2

这是更改选择列表中选项的颜色、背景和字体大小的答案。

<style>
    .color-and-font 
    {
       font-size: 16px;
       color: rgb(44, 44, 44);
       background: #b4b3b3;
    }
</style>
<div class="control">
    <label for="c_color_id" > 选择颜色代码 </label>
    <select name="c_color_id[]" id="c_color_id_main">
        <option value="1" class="color-and-font">value 1</option>
        <option value="2" class="color-and-font">value 2</option>
        <option value="3" class="color-and-font">value 3</option>
        <option value="4" class="color-and-font">value 4</option>
    </select>
</div>
英文:

Here is your answer of changing color, back-ground and font size of options in a select list.

&lt;style&gt;
    .color-and-font 
    {
       font-size: 16px;
       color: rgb(44, 44, 44);
       background: #b4b3b3;
    }
&lt;/style&gt;
&lt;div class=&quot;control&quot;&gt;
    &lt;label for=&quot;c_color_id&quot; &gt; Select Color Code &lt;/label&gt;
    &lt;select name=&quot;c_color_id[]&quot; id=&quot;c_color_id_main&quot;&gt;
        &lt;option value=&quot;1&quot; class=&quot;color-and-font&quot;&gt;value 1&lt;/option&gt;
        &lt;option value=&quot;2&quot; class=&quot;color-and-font&quot;&gt;value 2&lt;/option&gt;
        &lt;option value=&quot;3&quot; class=&quot;color-and-font&quot;&gt;value 3&lt;/option&gt;
        &lt;option value=&quot;4&quot; class=&quot;color-and-font&quot;&gt;value 4&lt;/option&gt;
    &lt;/select&gt;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.color-and-font {
    font-size: 24px;
    color: red;
    background: #b4b3b3;
}

<!-- language: lang-html -->

&lt;div class=&quot;control&quot;&gt;
    &lt;label for=&quot;c_color_id&quot; &gt; Select Color Code &lt;/label&gt;
    &lt;select name=&quot;c_color_id[]&quot; id=&quot;c_color_id_main&quot;&gt;
        &lt;option value=&quot;1&quot; class=&quot;color-and-font&quot;&gt;value 1&lt;/option&gt;
        &lt;option value=&quot;2&quot; class=&quot;color-and-font&quot;&gt;value 2&lt;/option&gt;
        &lt;option value=&quot;3&quot; class=&quot;color-and-font&quot;&gt;value 3&lt;/option&gt;
        &lt;option value=&quot;4&quot; class=&quot;color-and-font&quot;&gt;value 4&lt;/option&gt;
    &lt;/select&gt;
&lt;/div&gt;

<!-- end snippet -->

</div>

答案2

得分: 0

抱歉,我不认为在其他浏览器中可能实现。然而,您可以使用CSS和JavaScript创建完全自定义的下拉菜单,然后可以以任何方式自定义样式。我真诚希望这对您有帮助。

英文:

Sorry bud, I don't think it's possible in other browsers. However you could create a completely custom drop down using CSS and JavaScript, then you can style everything in any way shape or form. I truly hope this helps.

答案3

得分: 0

如果你无法通过使用php解决问题,也许javascript可以为你提供解决方案。

尝试这样做:

let htmlCollection = document.getElementById("c_color_id_main").children;
Array.from(htmlCollection).forEach((element) => {
  console.log(element);
  // 进行一些操作
});

使用children会得到一个HTMLCollection,你需要将其转换为数组。
然后你可以遍历并设置你的样式。

英文:

If you cannot fix the problem by using php, maybe javascript is a solution for you.

Try this

let htmlCollection = document.getElementById(&quot;c_color_id_main&quot;).children;
      Array.from(htmlCollection).forEach((element)=&gt;{
        console.log(element);
        // do something
      });

With children you get a HTMLCollection, which you have to convert into an array.
Then you can iterate through and set your styles.

huangapple
  • 本文由 发表于 2023年2月8日 13:51:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定