2013-11-28 31 views
0

嗨我怎样才能让我的网页不会重新加载每次我点击另一个链接?Onclick页不应该重新加载

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> 
+0

试'的onclick = “返回myFunctionD( '3')”',但是当你设置'document.location'页是你为什么要设置自动重新加载 – Grundy

+1

'document.location'如果你不希望页面重新加载? – Quentin

+0

使用window.location –

回答

0

您可以更改目标,以打开新的标签或窗口的链接。使用window.open()代替window.location

0

其实你甚至不需要使用Ajax。你可以简单地加载其他页面的内容在您的当前页面jQuery的$不用彷徨功能

//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中的内容。

+0

该怎么做?你能给我一个样本吗?谢谢 – user3034828

+0

让我把一些代码w8 –

+0

我尝试你的代码,但它不会工作。 – user3034828

0

您可以使用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>