答案 0 :(得分:1)
以下是用户选择颜色时如何设置颜色的示例。这应该很容易理解。每当用户更改选择选项时,我都会获取值并设置#collection元素的背景。这是使用纯JS而不是jQuery完成的。
<?php
$colors = ['#ff0000','#00ff00','#0000ff'];
?>
<select onchange="colorChange()" id = "color-picker">
<?php
foreach ($colors as $color) {
echo "<option value='$color'>$color</option>";
}
?>
</select>
<div id="color-picked">
</div>
<style type="text/css">
#color-picker {
float: left;
height: 20px;
}
#color-picked {
width: 20px;
height: 20px;
border-radius: 10px;
float: left;
margin-left: 20px;
}
</style>
<script type="text/javascript">
function colorChange() {
colorPicker = document.getElementById('color-picker');
colorPicked = document.getElementById('color-picked');
colorPicked.style.background = colorPicker.value
console.log(colorPicker.value)
}
</script>