我創建了一個隨機引用機器,它將呈現來自各種哲學家的隨機引用。使用jQuery和Math.random()來選擇嵌套對象屬性
我有一個對象字面與嵌套對象包含哲學家和他們的引號。使用jQuery函數和Math.random(),我如何從我的對象字面結構中選擇一個隨機引號?有沒有更好的方法來組織數據?
我已經開始了一個jQuery的封閉,將顯示一個指定的報價,我想使用Math.random()修改。
尋找解決方案的解釋,因爲我是初學者。提前致謝。
實施例對象常量:
var quotes =
{
awatts: {
name: "Alan Watts",
quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance."
},
etolle: {
name: "Eckhart Tolle",
quote: "Realize deeply that the present moment is all you ever have."
},
tmckenna: {
name: "Terrence Mckenna",
quote: "「The cost of sanity in this society, is a certain level of alienation」 "
}
};
例jQuery函數與單引號選自:
$(document).ready(function() {
$('.mybutton').click(function() {
$('#quote').html(quotes.awatts.quote);
});
});
謝謝adeneo。這個解決方案對我很好!真的很感激迅速的迴應。 – Lewie