我有一列我想在框中显示的笑话。当页面被加载时,我想在框中随机显示一个。然后,当我点击框时,我希望它再次将值更改为列表中的另一个随机值。单击时更改按钮的值
目前我只是无法让它工作,不能解决我出错的地方。我有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/
如果我想使按钮一个div怎么办?我可以这样做吗? @Jan hancic –
当然,点击处理程序将是相同的。您只需使用.html()而不是.val() –