如何防止HTML按钮刷新整个页面
How do I prevent my HTML button from refreshing the entire page?
我构建了一个简单的句子生成器——当你点击一个HTML按钮时,它下面会生成一个随机的句子
但是,每次单击按钮时,整个页面都会刷新。我该如何防止这种情况,只需刷新按钮下面的句子?
这是按钮HTML:
<div class="wrap">
<center><button onclick="location.reload()">Click me</button></center>
</div>
以及句子出现的HTML:
<div class="container">
<h5></h5>
</div>
最后,这里是生成句子的脚本(不确定它是否相关(:
<script>
jQuery(document).ready(function() {
//declare arrays
var nouns = ['noun 1', 'noun 2'];
var clauses = ['clause 1'];
var names = ['name 1','name 2'];
var adjective = ['adjective 1'];
var reasons = ['reason 1'];
//shuffle through contents of each array, picking one entry per array
var randNoun = nouns[Math.floor(Math.random() * nouns.length)];
var randName = names[Math.floor(Math.random() * names.length)];
var randClause = clauses[Math.floor(Math.random() * clauses.length)];
var randScandal = scandals[Math.floor(Math.random() * scandals.length)];
var randReason = reasons[Math.floor(Math.random() * reasons.length)];
//place the random entry into the appropriate place in the HTML
jQuery("h5").html("");
jQuery("h5").append(randNoun + " ");
jQuery("h5").append(randClause + " ");
jQuery("h5").append(randName + " ");
jQuery("h5").append(randScandal + " ");
jQuery("h5").append(randReason);
});
谢谢!
您应该将代码放在一个函数中:
function generate(){
//declare arrays
var nouns = ['noun 1', 'noun 2'];
var clauses = ['clause 1'];
var names = ['name 1','name 2'];
var adjective = ['adjective 1'];
var reasons = ['reason 1'];
//shuffle through contents of each array, picking one entry per array
var randNoun = nouns[Math.floor(Math.random() * nouns.length)];
var randName = names[Math.floor(Math.random() * names.length)];
var randClause = clauses[Math.floor(Math.random() * clauses.length)];
var randScandal = scandals[Math.floor(Math.random() * scandals.length)];
var randReason = reasons[Math.floor(Math.random() * reasons.length)];
//place the random entry into the appropriate place in the HTML
jQuery("h5").html("");
jQuery("h5").append(randNoun + " ");
jQuery("h5").append(randClause + " ");
jQuery("h5").append(randName + " ");
jQuery("h5").append(randScandal + " ");
jQuery("h5").append(randReason);
}
然后点击按钮调用该函数HTML:
<center><button>Click me</button></center>
JS:
jQuery(document).ready(function() {
jQuery('button').on('click', function(){
generate();
//or you can have all the code here without a function
});
});
只需将整个jquery代码放在一个函数中,代码上的onclick="location.reload()
就会加载整个页面。移除它。-
<script> function sentenceLoad() {
//declare arrays
var nouns = ['noun 1', 'noun 2'];
var clauses = ['clause 1'];
var names = ['name 1','name 2'];
var adjective = ['adjective 1'];
var reasons = ['reason 1'];
//shuffle through contents of each array, picking one entry per array
var randNoun = nouns[Math.floor(Math.random() * nouns.length)];
var randName = names[Math.floor(Math.random() * names.length)];
var randClause = clauses[Math.floor(Math.random() * clauses.length)];
var randScandal = scandals[Math.floor(Math.random() * scandals.length)];
var randReason = reasons[Math.floor(Math.random() * reasons.length)];
//place the random entry into the appropriate place in the HTML
jQuery("h5").html("");
jQuery("h5").append(randNoun + " ");
jQuery("h5").append(randClause + " ");
jQuery("h5").append(randName + " ");
jQuery("h5").append(randScandal + " ");
jQuery("h5").append(randReason);
}
</script>
在html中,将按钮替换为-
<button onclick="sentenceLoad()">Click me!</button>
相关文章:
- 如何防止jQueryonclick事件中的Ruby方法在页面刷新时执行
- 防止用户多次刷新页面
- 防止在刷新长HTML页面时滚动跳转
- 如何防止下拉刷新移动chrome
- 如何防止刷新页面时加载点击事件
- Javascript:如何防止刷新时重置时间限制
- 如何使用 JavaScript 更新/更改 HTML 内容并防止页面刷新
- SmoothStateJS:如何防止元素在页面更改时刷新
- 如何防止页面在 AJAX 中刷新时转到主页
- 如何防止在单击提交按钮时刷新页面
- 如何在执行回发时防止页面刷新
- 防止刷新后返回不工作
- 如何防止我的计时器在浏览器刷新时重置
- 如何防止在单击确认警报框中的取消按钮后刷新页面
- 防止页面在通过JQuery提交表单时刷新
- 如何防止按钮在刷新时进入原始状态
- 如何防止 Javascript 更新整个控件和刷新内容
- 如何防止在分配权限后刷新html列表菜单项
- 如何防止验证后刷新页面
- 防止iFrame在添加第二个iFrame时刷新