从javascript写入div

时间:2014-03-12 04:04:20

标签: javascript html

我正在尝试创建一个div框,它显示在javascript中生成的随机引用,但在运行代码时它不会显示。

我的HTML只是

<div id="quotebox"></div>

我的javascript是

var quotes = new Array()
quotes[0]="Simplicity is the ultimate sopistication";
quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
quotes[2]="The greatest deception men suffer is from their own opinions.";
quotes[3]="Art is never finished, only abandoned";
quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

var randQuote=Math.floor(Math.random()*(quotes.length));
function showQuote(){
    document.getElementById("quotebox").innerHTML = quotes[randQuote];
}

我只是犯了一个愚蠢的错误或者发生了什么?我现在尝试了几种不同的东西,它仍然没有显示给div。

4 个答案:

答案 0 :(得分:1)

你没有调用showQuote();这是工作中的JS小提琴http://jsfiddle.net/9bmAN/

var quotes = new Array()
quotes[0]="Simplicity is the ultimate sopistication";
quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
quotes[2]="The greatest deception men suffer is from their own opinions.";
quotes[3]="Art is never finished, only abandoned";
quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

var randQuote=Math.floor(Math.random()*(quotes.length));
function showQuote(){
    document.getElementById("quotebox").innerHTML = quotes[randQuote];
}

showQuote()

答案 1 :(得分:0)

确保你有一个带有id的元素(div,td,等等):&#34; quotebox&#34;:     

function showQuote(){
   var quotes = new Array()
quotes[0]="Simplicity is the ultimate sopistication";
quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
quotes[2]="The greatest deception men suffer is from their own opinions.";
quotes[3]="Art is never finished, only abandoned";
quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

var randQuote=Math.floor(Math.random()*(quotes.length));
    document.getElementById("quotebox").innerHTML = quotes[randQuote];
}
</script>
<body onload="showQuote();">
<div id="quotebox">

</div>
</body>

答案 2 :(得分:0)

<script>
window.onload=function(){   
    var quotes = new Array()
    quotes[0]="Simplicity is the ultimate sopistication";
    quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
    quotes[2]="The greatest deception men suffer is from their own opinions.";
    quotes[3]="Art is never finished, only abandoned";
    quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

    var randQuote=Math.floor(Math.random()*(quotes.length));
        document.getElementById("quotebox").innerHTML = quotes[randQuote];

};     
  </script>

<body >
<div id="quotebox"></div>
</body>

答案 3 :(得分:0)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<script>
var quotes = new Array()
quotes[0]="Simplicity is the ultimate sopistication";
quotes[1]="While I thought that I was learning how to live, I have been learning how to die.";
quotes[2]="The greatest deception men suffer is from their own opinions.";
quotes[3]="Art is never finished, only abandoned";
quotes[4]="Iron rusts from disuse; water loses its purity from stagnation. Even so does inaction sap the vigor of the mind.";

var randQuote=Math.floor(Math.random()*(quotes.length));
function showQuote(){
    document.getElementById("quotebox").innerHTML = quotes[randQuote];
}
</script>
<body onload="showQuote()">
    <div id="quotebox"></div>
</body>
</html>

此代码在我的浏览器上运行良好。可能是您浏览器的一些问题。