我有一个笑话列表,我想在一个框中显示。加载页面时,我想在框中随机显示一个。然后,当我单击该框时,我希望它再次将值更改为列表中的另一个随机值。
目前我只是无法让它工作,也无法弄清楚我哪里出错了。我的 HTML 如下:
<input class="box" type="button" value"" />
<ul>
<li>How much do pirates pay to get their ear pierced? </br> A buck anear!</li>
<li>How do pirates talk to one another? </br> Aye to aye!</li>
<li>What did the sea say to the pirate captain? </br> Nothing, it justwaved!</li>
<li>What is a pirates favourite shop? </br> Ar-gos!</li>
<li>When is it the best time for a pirate to buy a ship? </br> Whenit is on sail!</li>
</ul>
脚本是:
$(document).ready(function () {
var list = $("ul li").toArray();
var elemlength = list.length;
var randomnum = Math.floor(Math.random() * elemlength);
var randomitem = list[randomnum];
$('.box').click(function () {
$(this).val(randomitem);
});
});
有人可以告诉我如何在页面加载时使按钮具有值,然后在单击按钮时更改值。
小提琴:http: //jsfiddle.net/uRd6N/98/