Dynamically selecting colors using php or javascript
can anyone pls give sugessions on how to write开发者_JAVA百科 the script for selecting a color in a live page so that the selected color apply to whole page.can any one pls provide me a sample scripts for dis.
Try using a jQuery Colorpicker like this one. I'm guessing that's what you mean by 'selecting'.
if you want to change the background color of your page dynamically with PHP here are the tips to start:
create a simple PHP page like this (ugly but fast solution):
<?php function getBackgroundColor() { switch(rand(0, 3)) { case 1: return "#f00"; case 2: return "#0f0"; case 3: return "#00f"; default: return "#fff"; } } ?> <html> <head><title>test page</title></head> <body style="background-color: <?php echo getBackgroundColor(); ?>"> here comes the body </body> </html>
you can create a template file for the PHP and do the same just put the HTML in a different file
- do with AJAX the PHP will answer only the color and your javascript will change the background color
If by "applying the color to the whole page" you are talking about changing the background color of the page, try something like:
<input type="text" id="col" value="Enter hex color here" />
<input type="button" value="Change" onclick="changeColor()"/>
<script>
function changeColor()
{
col = document.getElementById("col").value;
/*
Make sure the col is indeed a valid one.
Acceptable formats include:
#789abc
#xyz //expanded to #xxyyzz
rgb(0, 127, 255)
Standard colors like "red", "blue" etc.
*/
document.body.style.backgroundColor = col;
}
</script>
To avoid errors caused by ignorant user inputs, you can use a drop-down-box instead of the text-field and populate it with allowed values.
精彩评论