Javascript 通过仅重新加载一个div来显示随机列表项
我正在处理一个页面,其中显示一个Javascript 通过仅重新加载一个div来显示随机列表项,javascript,Javascript,我正在处理一个页面,其中显示一个。按下按钮时,页面将重新加载并随机显示另一个。我通过以下几点实现了这一点: this.randomtip = function(){ var length = $("#tips li").length; var ran = Math.floor(Math.random()*length) + 1; $("#tips li:nth-child(" + ran + ")").show(); 而且 <a href="#" class="m
。按下按钮时,页面将重新加载并随机显示另一个
。我通过以下几点实现了这一点:
this.randomtip = function(){
var length = $("#tips li").length;
var ran = Math.floor(Math.random()*length) + 1;
$("#tips li:nth-child(" + ran + ")").show();
而且
<a href="#" class="myButton" onClick="location.reload();">
当按钮重新加载页面时,几个元素跳转并落回原位。我试过用固定高度的div来固定这些元素。是否可以只重新加载这一个div以避免跳转?使用按钮上的单击事件隐藏提示并显示随机提示,而不是重新加载页面:
$(function(){
var $tips = $("#tips li");
$('.myButton').click(function(){
var rand = Math.floor(Math.random()*$tips.length);
$tips.hide().eq(rand).show();
return false;
}).triggerHandler('click'); //Trigger event on page load
});
这里有一个你真的需要重新加载页面吗?为什么不直接隐藏和显示列表项?