我正在學習一門課程,需要我製作「隨機報價生成器」。向我的功能添加班級
我想添加這個類:
div.info p {
color: #fff;
font-size: 1em;
}
我陣列我在我的jQuery的創建。我完全失去了。我試過添加
.addClass(div.info p)
我的代碼,我認爲這就是我應該這樣做,但不知道如何。
這裏是我的JS至今:
$(document).ready(function() {
function getQuote() {
var quotes = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p" ];
var author = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p" ];
$.each($('.info'), function(i, element) {
$(element).html(quotes[randomNum]);
});
var randomNum = Math.floor((Math.random() * quotes.length));
var randomQuote = quotes[randomNum];
var randomAuthor = author[randomNum];
$(".quote").text(randomQuote);
$(".author").text(randomAuthor);
}
$(".btn").on("click", getQuote);
});
所以,如果我正確地理解你,我導入ajax到我的外部codepen。 如果我有我的數組定義爲引號和randomQuotes。 這是否合理 $(「randomQuote」).addClass(「div.info p」)? 編輯:感謝您的快速回復! –
如果你使用[.addClass](https://api.jquery.com/addclass/)方法,你需要使用CSS類選擇器(這裏是一個例子)。當你的函數要迭代數組時,它將會自動用_.addClass_方法添加該樣式。 –
看起來你正在使用jQuery編輯HTML。我對麼? –