如何防止页面刷新按钮点击html5? [英] How to prevent page refresh on button click in html5?
问题描述
我想阻止按钮点击重新加载页面。单击我只想交换按钮值不刷新页面。
I want to prevent reloading the page on button click. On click I just want to swap the button values not refresh the page. How can I do so?
<button id="4">134</button> // button 1
<button id="5">234</button> // button 2
点击按钮1(ID为4的按钮)后,我想要以下结果
After clicking button 1 (button with id of "4") I want the following result
// After clicking button 1
<button id="4">234</button> // button 1
<button id="5">134</button> // button 2
如何在不重新加载/刷新html页面的情况下执行此操作。
How can I do this without reloading/refreshing the html page.
推荐答案
为了防止刷新,只需添加type =button
To prevent the refresh just add type="button"
<button id="4" type="button">134</button>
<button id="5" type="button">234</button>
至于交换值我重申bmargulies,你需要学习javascript,或者让你的问题更清楚我们可以给你一个更好的答案。
As for swapping values I reiterate bmargulies, you need to learn javascript, or make your question more clear so we can give you a better answer.
但是,由于你的模糊问题(指出单击#4后文本应该交换),这里有一些使用jQuery的方向。这样做只会交换一次。
But with your vague question (stating that after clicking #4 the text should swap) here is some direction using jQuery. Doing it this way will only swap it once.
<script type="text/javascript">
$(function(){
$('#4').click(function(){
$(this).html(234);
$('#5').html(134);
});
});
</script>
关于jQuery的帮助: http://api.jquery.com/
For help with jQuery: http://api.jquery.com/
这篇关于如何防止页面刷新按钮点击html5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!