英文:
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"> 选择颜色代码 </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
`<div class="control">
<label for="c_color_id" id="c_color_id_main_label"> Select Color Code &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=""> Please Add Colors from backend </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 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.
<style>
.color-and-font
{
font-size: 16px;
color: rgb(44, 44, 44);
background: #b4b3b3;
}
</style>
<div class="control">
<label for="c_color_id" > Select Color Code </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>
<!-- 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 -->
<div class="control">
<label for="c_color_id" > Select Color Code </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>
<!-- 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("c_color_id_main").children;
Array.from(htmlCollection).forEach((element)=>{
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论