简体   繁体   English

通过AJAX将PHP文件加载到DIV中

[英]Loading PHP file into DIV via AJAX

Say I have an index.php that loads a page.php into the div of class ' wrapper '. 假设我有一个index.php ,它将page.php加载到类' wrapper '的div中。

page.php in turn has its own links and I want it so that when you click on one of these links it loads the respective page into the SAME ' wrapper ' div . page.php又有自己的链接,我想要它,这样当你点击其中一个链接时,它会将相应的页面加载到相同的' wrapper ' div

Similarly that loaded page might have its own links and I want to continue loading content into the same div. 同样,加载的页面可能有自己的链接,我想继续将内容加载到同一个div中。 How might I achieve this chain effect of pages? 我怎样才能实现页面的链式效果?

jQuery / AJAX is your friend here: jQuery / AJAX是你的朋友:

$('#link').click(function() {
        var go = $.ajax({
            type: 'POST',
            data: {m: 'ajax', // POST Variables go here...
                linkID: $(this).val()} // $(this).val() is the value of the link clicked
        })
        .done(function(results) {
            $('#resultsDiv').html(results); // This is where your results go
        })
        .fail(function(msg) {
            alert("Error:" + msg);
        })
        .always(function() {
        });
    }

I would put the AJAX call into a function: 我会把AJAX调用放到一个函数中:

function loadWrapper($url){
    $('#wrapper').load($url+' > *',function(){
        $(this).on('click','a',function(e){
            e.preventDefault();
            loadWrapper($(this).attr('href'));
        });
    });
}

Then on page load assign the click to any item on the page that loads initially: 然后在页面加载时将点击分配给最初加载的页面上的任何项目:

$('.LinkClass').on('click',function(e){
    e.preventDefault();
    loadWrapper($(this).attr('href'));
});

And, of course, your original load: 而且,当然,你的原始负载:

loadWrapper('page.php');

The callback in the function will allow click events to fire on the loaded links, as well as any other links you may add in the future you want to load in .Wrapper . 函数中的回调将允许点击事件在已加载的链接上触发,以及您可能在将来要添加的任何其他链接.Wrapper Just give those links the class LinkClass (or whatever you want) and you're good to go. 只需给这些链接提供LinkClass类(或任何你想要的),你就可以了。

Using jQuery: 使用jQuery:

var activateLinks = function() {
    $('.wrapper a[href]').click(function(){
        $('.wrapper').load($(this).attr('href'), activateLinks);
        return false;
    });
}

$('.wrapper').load('page.php', activateLinks);

or else 要不然

$('.wrapper').on('click', 'a[href]', function() {
    $('.wrapper').load($(this).attr('href');
    return false;
}
$('.wrapper').load('page.php');

Using a delegate is best this way you dont have to run handler attavchment for the links on the new page. 使用委托是最好的,这样你就不必为新页面上的链接运行处理程序获取。 They are handled automatically. 它们会自动处理。

$(document).on('click', 'div.wrapper a[href]', 'click', function (e) {
    e.preventDefault();
    $.get($(this).attr('href'), function (html) {
       $('div.wrapper').html(html);
    });
});

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

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