简体   繁体   English

我应该如何在 a 中显示数组颜色列表<select>

[英]How should I show list of array colors in a <select>

I am a newbie, and I am not sure how to achieve what I want.我是新手,我不知道如何实现我想要的。 Well, I have this piece of code, which I am learning currently.好吧,我有这段代码,我目前正在学习。

var color = ["#AAAAAA" , "#FF9992", "#BF9232"];
$("#button1").click(function() {
  // I have a print a the colors in array in <select><option>

});

On clicking the button I would like to create a <select> tag with all the colours in the array, populated with the <option> 's background colour being the value of the colour.单击按钮时,我想创建一个<select>标记,其中包含数组中的所有颜色,并填充<option>的背景颜色作为颜色值。

I am not sure how I could proceed.我不确定如何继续。 All I know is it can be done using jQuery as the array is dynamic.我所知道的是它可以使用 jQuery 来完成,因为数组是动态的。 Thanks in advance.提前致谢。

You can do it using .map() function of the array.您可以使用数组的.map()函数来完成。 Use this way:使用这种方式:

 var color = ["#AAAAAA" , "#FF9992", "#BF9232"]; $("#button1").click(function() { $("div").append("<select />"); var html = ""; color.map(function (clr) { html += '<option style="background: ' + clr + ';">' + clr + '</option>'; }); $("select").append(html); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <button id="button1">Click</button> <div></div>

If you want the <select> to change the background colour based on the <option> , then you can do this:如果您希望<select>根据<option>更改背景颜色,则可以执行以下操作:

 var color = ["#AAAAAA" , "#FF9992", "#BF9232"]; $("#button1").click(function() { $("div").append("<select />"); var html = ""; color.map(function (clr) { html += '<option style="background: ' + clr + ';">' + clr + '</option>'; }); $("select").append(html).change(function () { $(this).css("background", $(this).val()); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <button id="button1">Click</button> <div></div>

I suggest other solution with plain JavaScript.我建议使用纯 JavaScript 的其他解决方案。

 var color = ["#AAAAAA" , "#FF9992", "#BF9232"]; function addOptions(arr, selectId) { var s = document.getElementById(selectId); if (!s) { s = document.createElement("select"); var myDiv = document.getElementById("box"); myDiv.appendChild(s); s.id = selectId; } s.options.length = 0; for (var i = 0; i < arr.length; i++) { var option = document.createElement('option'); option.text = arr[i]; option.style.backgroundColor = arr[i]; option.value = arr[i]; s.options[s.options.length] = option; } } document.getElementById("button").onclick = function () { addOptions(color, "second"); };
 <button id="button">Click me</button> <div id="box"></div>

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

相关问题 我应该如何用 css 显示选择标题 - How should i show select title with css 我在数组中有列表。,如何根据映射中的条件显示其元素之一应首先出现? - I have list in the array .,how do I show one of its element should come first based on condition in mapping? 如何显示数组的列表元素? - How do I show a of list elements of an array? 如何让数组显示在无序列表中? - How do I get array to show up in an unordered list? 只有选择了另一个选择(父项)后,我才能显示选择列表 - How i can show a select list only after another select (parent) has been selected 我应该在GraphQL中使用列表(数组)吗? - Should I be using a list (array) here in GraphQL? 如何在角度材质的对话框内容中显示下拉列表/下拉列表/选择列表? - How do I show a dropdown/dropdown list/select list inside of a angular material's dialog's content? 我应该如何从Angular-2的下拉列表中将数组与多个选择选项绑定 - How should i bind array with multi select options from dropdown in Angular-2 如何让IE 10显示指针而不是选择列表的I-bar? - How do I get IE 10 to show a pointer instead of an I-bar for a select list? Vuejs单击打开按钮时如何显示选择列表 - Vuejs How can I show select-list when I click on open button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM