试图让stylize()使用CSS样式分别随机选择单元格bgcolor和文本样式

时间:2013-08-30 14:40:34

标签: javascript html css

这是代码。随机字体样式工作,但我无法使随机背景颜色工作。我看到有一段时间的帖子几乎就像这样,但我太新了,无法理解如何使用他们的答案来修复我的。 previous post

<html>

<head>
    <title>HTML and JavaScript</title>
        <link href="js-twentyfive.css" rel="stylesheet" type="text/css"></link>

<script>
var index = 0;
function stylize() 
{
    if (index > 20) index = 1;
    var s = "myStyle" + index;
    var e = document.getElementById("MessageText")
    e.className = s
    {
var bgindex = 0;
    bgindex++;
    if (bgindex >5) bgindex =1;
    var b ="myBackground" + bgindex;
    var c = document.getElementById("MessageCell")
    c.className = b
    }
}
function getRandomInt (min, max)
{
    return Math.floor(Math.random() *(max - min +1)) + min;
}
index = getRandomInt(1, 20);
bgindex = getRandomInt(1, 5);
</script>
</head>

<body onLoad="stylize()">
    <table align="center" border="1" bordercolor="black">
        <tr>
            <td align="center">
                <font size="3"><b>STYLE CLASS VIEWER</b></font>
            </td>
        </tr>
        <tr>
            <td id="MessageCell" align="center" height="100" width="400">
                <div id="MessageText">
                    Hello World Wide Web!
                </div>
            </td>
        </tr>
    </table>
</body>

<html>

这是我的CSS:

.myBackground1{color:blue}
.myBackground2{color:red}
.myBackground3{color:purple}
.myBackground4{color:yellow}
.myBackground5{color:orange}

.myStyle1 {color:black; font-size:12}
.myStyle2 {color:red; font-family:Lucida Grande;font-size:14}
.myStyle3 {color:blue; font-family:serif;font-size:16}
.myStyle4 {color:green; font-family:times; font-size:18}
.myStyle5 {color:yellow; font-family:monospace; font-size:22}
.myStyle6 {color:orange; font-family:"Brush Script MT", cursive;font-size:12}
.myStyle7 {color:cyan; font-size:14}
.myStyle8 {color:purple; font-size:16}
.myStyle9 {color:pink; font-size:18}
.myStyle10 {color:lightred; font-size:22}
.myStyle11 {color:lightblue; font-size:12}
.myStyle12 {color:White Orchid; font-size:16}
.myStyle13 {color:deepskyblue4; font-family:Copperplate; font-size:14}
.myStyle14 {color:firebrick; font-size:18}
.myStyle15 {color:green4; font-size:12}
.myStyle16 {color:lightcoral; font-size:14}
.myStyle17 {color:black; font-size:16}
.myStyle18 {color:black; font-family: Papyrus, fantasy; font-size:18}
.myStyle19 {color:magenta; font-size:14}
.myStyle20 {color:black; font-size:16};

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您无法更改后台正确吗?您需要修复以下CSS样式:

.myBackground1{color:blue}
.myBackground2{color:red}

等...

.myBackground1{background-color:blue}
.myBackground2{background-color:red}

等...

并更改代码:

function stylize() 
{
    if (index > 20) index = 1;
    var s = "myStyle" + index;
    var e = document.getElementById("MessageText")
    e.className = s
    {
var bgindex = 0;
    bgindex++;
    if (bgindex >5) bgindex =1;
    var b ="myBackground" + bgindex;
    var c = document.getElementById("MessageCell")
    c.className = b
    }
}

function stylize() 
{
    index = getRandomInt(1, 20);
    var s = "myStyle" + index;
    var e = document.getElementById("MessageText")
    e.className = s


    bgindex = getRandomInt(1, 5);
    var b ="myBackground" + bgindex;
    var c = document.getElementById("MessageCell")
    c.className = b
    alert(b);
}

您的功能未考虑bgIndex中1-5的随机值。它始终将其设置为1,因此在修复CSS之后,您将始终拥有蓝色背景