我試圖讓刷新一個簡單的文字變化:上刷新
<strong>Title</strong>
<p id="myQuote">Slogan</p>
而就刷新了「口號」部分的變化:
var myQuotes = new Array();
myQuotes[0] = "To be or not to be";
myQuotes[1] = "The only thing we have to fear is fear itself";
myQuotes[2] = "Give me liberty or give me death";
var myRandom = Math.floor(Math.random()*myQuotes.length);
$('#myQuote').html(myQuotes[myRandom]);
你確保jQuery是進口的,你的代碼被稱爲在DOM準備回調?你有錯誤嗎? –
適用於我:http://codepen.io/joe/pen/BsoGH – EricG
在加載DOM之前,您無法選擇DOM元素;將'$('#myQuote')。html(myQuotes [myRandom]);'放入'$(document).ready(function(){...})'中。 – apsillers