问题
Stackoverflow is awesome! I just posted a question and it was answered by the community within minutes and a working answer was obtained!
I have another question about a custom color picker. I know there are many color picker out there but it is just too complicated to use. I need a simpler one.
So I decided to create a simple 4x4 pallet of predefined color. The pallets are formed using div with different background color under a div-class named 'supercolor'.
<div class="supercolor" >
<div class="colordiv" id="#111111" style="background-color:#111111;"> </div>
<div class="colordiv" id="#222222" style="background-color:#222222;" > </div>
<div class="colordiv" id="#333333" style="background-color:#333333;"> </div>
</div>
In my script section, I manually add on click function to each and individual id to dynamically create an (input option check box checked html) and a tick character on the division so the user knows that color is selected. If the division is already checked, it will removed the (input option check box checked html) and the tick character.
I used an array to determined if the div is checked, and if it is, update that array index. The input check box are created so when the page submits, I have a way to know which color was selected based on the check box value which is the background color in hex.
var selected_arry = [];
$('#111111').click(function(){
if (selected_arry == 1){
selected_arry[0] = 0;
$('#111111').html("");
} else {
selected_arry = 1;
$('#111111').html("✓<input type='checkbox' name='selected_color[]' hidden checked id='#111111' />");
}
});
->repeat same code for next 15 divs but with different ID
My question is, I have to repeat this for all the division I create and it does look very unoptimized and I think there must be a better way to do it. I only have a few months of Jquery exposure and a newbie developer. I am hoping all the gurus out there can point me to a more efficient way.
Thank you very much!
Edit : Working Code
Finally with the help from @egis & @Rob Cowie, the code is completed with very efficient and scalable function. Note: I removed some part (making it simpler for beginner like me to understand) and edited some part to allow multiple selection.
CSS Code:
<style type="text/css">
.colour {
width: 40px;
height: 40px;
float: left;
cursor: pointer;
line-height: 40px;
text-align: center;
}
.red {background-color: #FF2622;}
.green {background-color: #2B6DB9;}
.blue {background-color: #4AFF41;}
</style>
HTML Code:
<div id="colour-picker">
<div class="colour red"> </div>
<div class="colour green"> </div>
<div class="colour blue"> </div>
</div>
Script Code:
$(document).ready(function() {
$('.colour').click(function(event){
var $this = $(this);
$this.toggleClass('selected');
if ($this.hasClass('selected')){
var colour = $this.css('backgroundColor');
$this.html("✓<input type='checkbox' name='selected_color[]' hidden checked id='"+colour+"' value='"+colour+"' />");
} else {
$this.html('');
};
});
});
回答1:
I've created a solution that delivers what I think you're after. See https://gist.github.com/962872 for the code.
In summary, clicking on colour divs toggles the class 'selected' and sets the value of a hidden form input (which will submit the colour).
If the clicked div is currently selected, the class is removed, and the value of the hidden input is set to empty.
You can do more on click simply by adding to the click handler.
回答2:
Pass event to event handler, like this .click(function(event){ ... });
.
Then inside event function get the color attribute like this var color_hex = $(event.target).css('bacground-color');
. This code is untested and syntax might be wrong, I can't test it right now, but I think I shown you the right path ;)
Some example:
$('.supercolor').click(function(event){ // bind event to parent
var $target = $(event.target);
if ($target.hasClass('colordiv'){ // check if color div is clicked
if ($target.html() !== ''){ // check if it has some html inside, if so - it means the div has been clicked before
$target.html(''); //clear the html ("unclick")
}
else{ // div hasn't been clicked before
var color = $(event.target).css('background-color'); //get the color
$(event.target).html("✓<input type='checkbox' name='selected_color[]' hidden checked id='"+color+"' value='"+color+"' />"); // create html
}
}
});
来源:https://stackoverflow.com/questions/5932850/jquery-help-to-create-the-simplest-color-picker-ever