Javascript Onclick页面不应重新加载
嗨,我怎样才能使我的页面不在每次单击另一个链接时重新加载Javascript Onclick页面不应重新加载,javascript,Javascript,嗨,我怎样才能使我的页面不在每次单击另一个链接时重新加载 function myFunctionD(id) { var x=document.getElementById(id); var myURL = "http:www.sample.php"; document.location = myURL + "?t_id=" + x.id ; return false } HTML: 您可以更改目标以在新选项卡或窗口中打开链接。使用window.open()而不是w
function myFunctionD(id) {
var x=document.getElementById(id);
var myURL = "http:www.sample.php";
document.location = myURL + "?t_id=" + x.id ;
return false
}
HTML:
您可以更改目标以在新选项卡或窗口中打开链接。使用window.open()
而不是window.location
实际上,您甚至不需要使用Ajax。您只需使用Jquery的$.get函数在当前页面中加载另一个页面的内容即可
//Inserting a div for holding another page content. On click of button, this div will update content
<div id="contentdiv"></div>
<input type="button" value="Page 1 Load Content" onclick="loadContent('page1.html')"/>
<input type="button" value="Page 2 Load Content" onclick="loadContent('page2.html')"/>
<input type="button" value="Page 3 Load Content" onclick="loadContent('page3.html')"/>
<script type="text/javascript">
function loadContent(page)
{
//Empty contentdiv for new content and add new page content with jquery's $.get
$('#contentdiv').empty();
$.get('http://www.example.com/'+page, function(data) { $("#contentdiv").append(data); });
//This will load page content in your contentdiv
//for more info about $.get visit this http://api.jquery.com/jQuery.get/
}
</script>
//插入一个div以保存另一个页面内容。单击按钮,此div将更新内容
函数加载内容(第页)
{
//为新内容清空contentdiv,并使用jquery的$.get添加新页面内容
$('#contentdiv').empty();
$.get('http://www.example.com/“+页,函数(数据){$(“#contentdiv”).append(数据);});
//这将在contentdiv中加载页面内容
//有关$的详细信息,请访问此http://api.jquery.com/jQuery.get/
}
在其他页面中,只需将要加载的内容放入contentdiv即可。不要用html和正文标记等创建完整的html页面。只需在contentdiv中显示您想要的内容。您可以使用类似于以下内容的ajax
<script type="text/javascript">
$(function(){ myFunctionD('3'){
var form = $(this).parent("form");
$.ajax({
type: "POST",
url: form.attr("yourform"),
data: form.serialize()
})
return false;
});
});
</script>
$(function(){myFunctionD('3')){
var form=$(this.parent(“form”);
$.ajax({
类型:“POST”,
url:form.attr(“yourform”),
数据:form.serialize()
})
返回false;
});
});
尝试onclick=“return myFunctionD('3')”
,但是当您设置document.location
页面自动重新加载时,您为什么要设置document.location
如果您不想重新加载页面?当我单击另一个链接时它不会更改url您想在单击按钮时更改顶部地址栏url吗?是,我的代码是每次点击一个链接,我的url被更改,如果url被更改,我想避免刷新页面。我不知道该怎么办?这是不可能的,你想改变网址,但阻止页面重新加载。我不知道你为什么要这样做,但如果你想让你的客户知道他目前在哪一页上,那么你可以有其他选择,比如面包屑样式。
<script type="text/javascript">
$(function(){ myFunctionD('3'){
var form = $(this).parent("form");
$.ajax({
type: "POST",
url: form.attr("yourform"),
data: form.serialize()
})
return false;
});
});
</script>