问题
This is regarding this question i asked on this link:PHP CSS Colour Shuffle array?
I have changed and added more code and have a different question hence why i've opened a new thread.
I am using php and jQuery to shuffle colours every time the page is refreshed. At the moment this works fine. But i have a jQuery function where i grab some elements and on a hoverstate show the shuffle colour. Now this does work but it's showing css standard default red,blue,green ect and not even displaying colours for the orange. I have been advised to create a variable and then create an if function and pass the variable through the hover function. Below is what i have so far, im a novice user at jQuery so i am unsure of where i am going wrong or where to go next, can someone guide me to why this is broken? My firebug shows no errors and i think im nearly there! Thanks.
PHP Shuffle colours:
<?php
$colours = array('red', 'green', 'blue', 'black');
$blog = $colours[array_rand($colours, 1)];
?>
<body id="<?php echo $blog; ?>">
In my css wherever i wanted to show the shuffle colour #green, #red is before the actuallid,class or element and it works! Just the hover states now where i have this jQuery..
// colour rollover navigation
var colours = "<?php echo $colours; ?>";
if(colours == "red"){
colours = "#600";
}
else if(colours == "green"){
colours = "#363";
}
else if(colours == "blue"){
colours = "#369";
}
else if(colours == "black"){
colours = "#000";
}
//example of some elements that would use the hover effect
$("#footer .address #breadcrumbs a").hover(function() {
$(this).stop().animate({ 'color': colours }, 300);
},function() {
$(this).stop().animate({ 'color': "#fff" }, 300);
});
Am i missing some lines in the if else or passing the variable through wrong? Thanks!
回答1:
I presume the JS is in script tags on the page.
You can't just echo out an array. I'm guessing you meant to echo $blog. That should pass a single color as string data to be picked up by your if () {} else {} statements (should be switch () { case } really).
回答2:
Ok i have it working.
I did some things so not sure exactly what totally fixed it:
-Clear Cache (Google Chrome is a pain for this)
-Changed the variable from Colours to color (both in php and JS)
-When defining the variable in JS i echoed $blog instead of $colour.
I get no errors when this works as well.
Thanks!
来源:https://stackoverflow.com/questions/15904993/jquery-if-else-colour-shuffle-passing-a-variable