dongyan1993 2015-12-26 23:49
浏览 17
已采纳

其他ajax中的Ajax请求

So i made an ajax request for get my "home" page and my "about" page in a "container" on click menu link button inside my index.php, and now i have three link in my "home" page, and i also want open each of these links inside my div "container" to replace the "home" page, so how can i make an ajax request after the first ajax call ?

This is my php request in the index.php:

<div id="container">
   <?php
    $d="contenu/";
    if(isset($_GET['p'])){
    $p=strtolower($_GET['p']);
    if(preg_match("/^[a-z0-9\-]+$/",$p) && file_exists($d.$p.".html")){
        include $d.$p.".html";
    }
        else{
            include "pages/404.html";
        }
        }
            else{
            include "pages/home.html";
        }
   ?>
</div>

In here my ajax:

$(document).ready(function(){
    $("#menu a").click(function(){
        page=$(this).attr("href");
        $.ajax({
            url: "pages/"+page,
            cache:false,
            success:function(html){
                afficher(html);
            },
            error:function(XMLHttpRequest,textStatus, errorThrown){
                afficher("erreur lors du chagement de la page");
            }
        });
        return false;
    });
    
});

function afficher(data){
$("#container").fadeOut(500,function(){
    $("#container").empty();
    $("#container").append(data);
    $("#container").fadeIn(1200);
    
});
}

and finally my home.html ( i just show you the links ):

<div class="section vs-section" data-speed="0.4">
            <div class="vs-transform title " data-speed="0.4"><a href="projet1.html"><h3>projet1</h3></a></div>
            <div class="vs-transform title" data-speed="0.38"><a href="projet2.html"><h3>Projet2</h3></a></div>
            <div class="vs-transform title" data-speed="0.4"><a href="projet3.html"><h3>projet3</h3></a></div>

  </div>

</div>
  • 写回答

1条回答 默认 最新

  • dongli1887 2015-12-27 00:21
    关注

    Yes you can, you should just use event delegation on() to deal with the new HTML tags added dynamically to the DOM by the first ajax request :

    $('body').on('click', '.vs-transform a', function(){
         //Second ajax request
    })
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?