dongliehuan3925 2015-12-08 09:40
浏览 32
已采纳

如何从同一div中的外部页面加载链接

Need help..

I have an index.php page. On that page, I have a link to page-2.php.

On page-2.php,

i also have a link to other page. May I know how to load the link on page-2.php in a same div on index.php page.

I'm using jQuery load method do to this. I'm able to load page-2.php inside a div on index.php. Below are the codes that I made.

$(document).ready(function() {
 $('a#staff_info').click(function(e){ //link to page-2.php
  e.preventDefault();
  var page = $(this).attr('href');
  $('#content').load(page);
  $('a#upd_staff').click(function(){ //link on page2.php 
     var page_info = $('a#upd_staff').attr('href');
     $('#content').load(page_info);
  });   
 }); 
});

the above code will direct me to a new page not on a same div

  • 写回答

3条回答 默认 最新

  • 普通网友 2015-12-08 10:02
    关注

    Ajax is asynchronous.

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

    The above starts loading page two

    $('a#upd_staff').click(function(){ //link on page2.php 
    

    Then you try to bind your event handler to content from page 2 before it is has finished loading.

    So when you click on the link, there is no JS bound to it, so it acts as a normal link.


    Use deferred event handlers (so they listen on the document and check what element was clicked when the click happens).

    $(document).ready(function() {
        function ajaxLoad(event) {
            event.preventDefault();
            var page = $(this).attr('href');
            $('#content').load(page);       
        }
        $(document).on("click", "a#staff_info, a#upd_staff", ajaxLoad);
     });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False