简体   繁体   English

导航时如何在不刷新页面的情况下将用户重新定位到另一页面的内容?

[英]How do I relocate the user to contents of another page without page refresh when navigating?

Im using javascript - ajax and jquery to load all contents from php files which is under (#menu a)[see below 'you.php'] without refreshing the page when navigating across the page - which works perfectly. 我正在使用JavaScript-ajax和jquery从(#menu a)[请参见以下'you.php']下的php文件加载所有内容,而无需在浏览页面时刷新页面-效果很好。

However, how do I create a hyperlink of somesort on content-A (which loads and shows all the contents from home.php) when clicked, it relocates & shows the user to/the contents of settings.php(B). 但是,如何在单击时在content-A上创建某种超链接(从home.php加载并显示所有内容),它会将用户重新定位并显示给settings.php(B)的内容。

Please note my href hyperlinks doesn't have .php at the end. 请注意,我的href超链接末尾没有.php。 The 'general.js' file explains why. “ general.js”文件说明了原因。

(you.php): (you.php):

        <div id="menu">

            <a href="home">Home</a>

            <a href="settings">Settings</a> // Content (A)

        </div>

        <div id="content"><div>

        <script src="./js/jquery-2.1.4.min.js"></script>

        <script src="./js/general.js"></script>

(general.js): (general.js):

$(document).ready(function() {

// initial content that will be loaded first upon logging in:

$('#content').load('home.php');

// handle menu clicks

$('#menu a').click(function(){

    var page = $(this).attr('href');

    $('#content').load(''  + page +  '.php');

    return false;

    });

});

(home.php): (home.php):

 <h1> welcome to homepage </h1>

 Would you like to go to your settings?

 Click here: <a href="settings.php>Settings</a> 

Obviously the problem with doing the href hyperlink like this in home.php, is that it goes directly to the settings.php page. 显然,在home.php中像这样执行href超链接的问题是,它直接进入了settings.php页面。 Which makes my general.js (ajax) and jquery file pointless. 这使我的general.js(ajax)和jquery文件毫无意义。 Since the whole point of using ajax and jquery is for smooth navigation and no page refresh upon navigating around the website. 由于使用ajax和jquery的全部目的是为了实现流畅的导航,并且在浏览网站时不会刷新页面。

and No, I do not want to load the contents of settings.php within the contents of home.php, 'loadception' is not what I'm looking for. 不,我不想在home.php的内容中加载settings.php的内容,“ loadception”不是我想要的。

This is my simple question, I would like a simple answer in php,javascript,ajax. 这是我的简单问题,我想用php,javascript,ajax中的简单答案。

Any ideas? 有任何想法吗?

You need to use event delegation. 您需要使用事件委托。 To better performance in my example, all links that must be loaded into "#content" have a class "open" so, in jquery you could do some like this: 为了提高性能,在我的示例中,必须加载到“ #content”中的所有链接都具有“ open”类,因此,在jquery中,您可以执行以下操作:

$('#content').on('click', '.open', function(e) {
    e.preventDefault();

    var page = $(this).attr('href');
    $('#content').load(''  + page +  '.php'); // concat .php if it's only necessary
});

Updated I created a demo on github: https://github.com/josemato/stackoverflow/tree/master/spa-event-delegation 更新后,我在github上创建了一个演示: https : //github.com/josemato/stackoverflow/tree/master/spa-event-delegation

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

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