![](/img/trans.png)
[英]Anchor onclick should not reload the page but trigger javascript?
[英]Onclick page should not reload
嗨,如何使我的頁面在每次單擊另一個鏈接時都不會重新加載?
function myFunctionD(id) {
var x=document.getElementById(id);
var myURL = "http:www.sample.php";
document.location = myURL + "?t_id=" + x.id ;
return false
}
HTML:
<a href="docview.php?id=26" id="3" onclick="myFunctionD('3')" target="iframe_a" >July 17, 2013</a>
<a href="docview.php?id=26" id="4" onclick="myFunctionD('4')" target="iframe_a" >July 18, 2013</a>
您可以更改目標以在新選項卡或窗口中打開鏈接。 使用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>
在其他頁面中,只需將要加載的內容放入contentdiv。 不要使用html和body標簽等制作完整的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.