【发布时间】:2018-01-16 18:25:41
【问题描述】:
我正在创建一个随机引用机器,它将显示来自不同哲学家的随机引用。
我有一个带有嵌套对象的对象文字,其中包含哲学家及其引用。使用 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);
});
});
【问题讨论】:
标签: javascript jquery random javascript-objects