简体   繁体   English

使用AJAX在页面之间导航

[英]Using AJAX to navigate between pages

I've implemented a JS solution to navigate between pages but now, I realize that's not enought for my needs. 我已经实现了一个JS解决方案以在页面之间导航,但是现在,我意识到这不足以满足我的需求。

I'd like to navigate between pages using AJAX, and my first idea was this one: 我想使用AJAX在页面之间导航,而我的第一个想法是:

$(document).ready(function() {
    //initial
    $('#main').load('content/index.php');

    //M. clicks
    $('.menu_pc li a').click(function(){
        var page = $(this).attr('href');
        $('#main').load('content/' + page + '.php');
        return false;
    });
});

Where #main it's the ID of the HTML section. #main是HTML部分的ID。

What happen if I use this method? 如果使用这种方法会怎样? Well, that I'm kind of limited because I should handle every click using the same method and I'm not sure if it will even work or not (first time using this). 好吧,我有点局限性,因为我应该使用相同的方法来处理每次点击,而且我不确定它是否会起作用(第一次使用此方法)。

For instance, if I have to recharge the header after some login from a user showing him his own name, I probably have problems using that method. 例如,如果我在显示用户自己名字的用户登录后必须为标题充值,则使用该方法可能会遇到问题。 That's why I need to use some REAL AJAX code. 这就是为什么我需要使用一些REAL AJAX代码的原因。

Like replace this form for a welcome message like "Hello user ". 就像用此表格替换欢迎消息(如“ Hello user ”)一样。

在此处输入图片说明

Some hint? 有什么提示吗?

Thanks in advance. 提前致谢。

You can use this method. 您可以使用此方法。 But it depends on what is your purpose? 但这取决于您的目的是什么? If you want to create very simple website with several pages it will be ok. 如果您想创建一个包含多个页面的非常简单的网站,则可以。

But if you plan to create something bigger, it will be hardly scalable if you will do it that way. 但是,如果您打算创建更大的东西,那么这样做将很难扩展。

So if your purpose is to create a big and easily scalable website, then consider using for example angular or emberjs. 因此,如果您的目的是创建一个大型且可轻松扩展的网站,请考虑使用例如angular或emberjs。

You could just use 你可以用

$(document).ready(function() {
  //initial
  $('#main').load('content/index.php');

  //M. clicks
  $('.menu_pc li a').click(function(){
    var page = $(this).attr('href');
    var redirectUrlPath = 'content/' + page;
    window.location.pathname = redirectUrlPath;
    return false;
  });
});

I'm not sure exactly what you want, but this will redirect you to a different page. 我不确定您到底想要什么,但这会将您重定向到另一个页面。 Assuming your A tag looks something like this: 假设您的A标签看起来像这样:

<a href='myNewPage'>link1</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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