jQuery 没有刷新的 Ajax 加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24980021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:43:32  来源:igfitidea点击:

Ajax load page without refresh

jqueryajaxrefresh

提问by Julius J

I am looking for information what is the simplest way to update page content using jquery + ajax. I would like to update only #containercontent, but not a full page.

我正在寻找信息,使用 jquery + ajax 更新页面内容的最简单方法是什么。我只想更新#container内容,而不是整页。

I am begginer in ajax.

我是 ajax 的初学者。

Thanks.

谢谢。

MY Jquery code

我的 Jquery 代码

  <script>
    $(document).ready(function(){
      //page
      $('.row').load('services.html');

      $('.nav_link').click(function(){
        var page = $(this).attr('href');
        $('.row').load(page);
        return false;
      });
    });
  </script>

HTML:

HTML:

<div class="row">
          <nav role="navigation" class="nav">
            <a class="nav_link" href="services"><div class="nav_line"></div>PASLAUGOS</a> <a class="nav_link" href="contacts.html"><div class="nav_line"></div>KONTAKTAI</a>
          </nav>
        </div>

services.html:

服务.html:

<div class="row">
    <nav role="navigation" class="nav">
        <a class="nav_link" href="services.html">
            <div class="nav_line"></div>
            PASLAUGOS
        </a> 
        <a class="nav_link" href="contacts.html">
            <div class="nav_line"></div>
            KONTAKTAI
        </a>
    </nav>
    <div class="result">
      gsdgdsgsdgdsgds gds gdsg dsg sd gds g sdgsd gdsg sd gsd
    </div>
</div>

回答by mplungjan

Try

尝试

$('.row').on("click",".nav_link",function(e){ 
  e.preventDefault(); // cancel click
  var page = $(this).attr('href');   
  $('.row').load(page);
});

Since .on will assign the click to the new links

由于 .on 会将点击分配给新链接