【问题标题】:Random background colour picker for tumblrtumblr 的随机背景颜色选择器
【发布时间】:2013-08-08 01:22:54
【问题描述】:

我的代码中有这个,它应该在每次刷新时选择一个随机的背景颜色,但我看不出我的 document.write 有什么问题

var bgcolorlist=new Array("#ff871b", "#15efa1", "#51ddff", "#ff1b6c", "#000000")



document.write('<meta name="color:Background" content='+background=bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)] + '>')

干杯!

【问题讨论】:

    标签: jquery html css tumblr


    【解决方案1】:

    每行后面都需要分号。

    另外,为什么你会看到带有元标记的背景颜色?

    改用这一行:

      document.body.style.backgroundColor = bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];
    

    【讨论】:

    • 他使用元名称可能是因为 Tumblr HTML 编辑器。
    【解决方案2】:

    另一种解决方案是在您的 body 中添加一个 ID,然后使用以下代码:

    <script>
        var bgcolorlist=new Array("background: #ff871b", "background: #15efa1", "background: #51ddff", "background: #ff1b6c", "background: #000000");
        var color = bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];
        var d = document.getElementById("your-body"); 
        d.setAttribute("style", color);
    </script>
    

    据我所知,Tumblr 允许您使用 jQuery,这比纯 Javascript 容易得多。如果您在代码中添加了 jQuery,只需执行以下操作:

    <script>
        var bgcolorlist=new Array("background: #ff871b", "background: #15efa1", "background: #51ddff", "background: #ff1b6c", "background: #000000");
        var color = bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];
        $('body').css('backgroundColor', color);
    </script>
    

    【讨论】:

    • 对不起,我的代码知识还比较初级。我会在我的 CSS 中添加什么来完成这项工作?感谢您的固定代码和 jQuery!
    • 在 Tumblr HTML 编辑器上的 HTML 模板中,搜索 (CRTL+F) 以查找 .
    猜你喜欢
    • 2013-10-14
    • 2018-10-13
    • 1970-01-01
    • 2021-04-03
    • 2014-01-19
    • 2018-11-25
    • 1970-01-01
    • 2021-12-19
    • 2014-02-11
    相关资源
    最近更新 更多