简体   繁体   English

刷新时更改背景颜色

[英]Change background color on refresh

I want my site to have a different background color for the body every time I refresh the page. 我希望每次刷新页面时,网站的正文背景颜色都不同。 Can you please tell me how this would be done using Javascript or PHP? 您能告诉我如何使用Javascript或PHP完成此操作吗? Thank you. 谢谢。

So simple with this function: 使用此功能非常简单:

With PHP 使用PHP

<?php

function random_bg() {
    return strtoupper(dechex(rand(0,10000000)));
}

?>

<body style="background-color: #<?php echo random_bg();?>">

With jQuery 使用jQuery

$(document).ready(function () {
    var color = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
    $('body').css('background', color);
});

You could do this on browser end using JavaScript with something like keep an array of colors and select one from that. 您可以在浏览器端使用JavaScript进行此操作,例如保留多种颜色并从中选择一种。 Using array of selected colors will ensure that you dont run in to some obscure color that may hamper the readability completely. 使用所选颜色的数组将确保您不会遇到一些可能完全妨碍可读性的晦涩的颜色。

var bgColors=new Array("#AAAAA", "#FFFFF", "#DDDFFF", "#CCCCCC", "#333333", "#FFCC11", "#FFFFFF", "#DDDD00")

document.body.style.background=bgColors[Math.floor(Math.random()*bgColors.length)]
<?php

  $color_list = array("green","red","white"); //colors that you wish

  bg = $color_list[rand(0,count($color_list))];

?>

<body bgclor="<?php echo bg; ?> ">

</body>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM