简体   繁体   English

Onclick页面不应重新加载

[英]Onclick page should not reload

Hi how can i make my page don't reload every time i click another link? 嗨,如何使我的页面在每次单击另一个链接时都不会重新加载?

function myFunctionD(id) { 
   var x=document.getElementById(id);   
   var myURL = "http:www.sample.php";
   document.location = myURL + "?t_id=" + x.id ;
 return false
}

HTML: 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>

You can change the target to open the link in new tab or window . 您可以更改目标以在新选项卡或窗口中打开链接。 Use window.open() instead of window.location 使用window.open()而不是window.location

Actually you don't even need to use Ajax. 实际上,您甚至不需要使用Ajax。 You can simply load content of another page in your current page with Jquery's $.get function 您可以使用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>

In your other pages, just put that content which you want to load into contentdiv. 在其他页面中,只需将要加载的内容放入contentdiv。 Do not make complete html page with html and body tags etc. Just content you want to appear in you contentdiv. 不要使用html和body标签等制作完整的html页面。仅是要显示在contentdiv中的内容。

You can use ajax something like this 您可以使用像这样的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM