【问题标题】:How to load website layout(header,footer) using js如何使用 js 加载网站布局(页眉、页脚)
【发布时间】:2017-03-28 16:32:51
【问题描述】:

我是 javascript 的新手。我想使用 ajax 加载我的网站主模板文件(页眉、页脚、侧边栏)。我的布局文件如下。
layout.php

<?php
   $app = new App;
   $ret = $app->header();
   $ret .= $app->sidebar();
   $ret .= $app->footer();
   echo json_encode($ret);
?>

现在我想使用 ajax 加载我的布局文件。因为最初没有 div 。如何加载这个。我的 js 文件如下所示。

$.ajax({
  url: 'layout.php',
  dataType: 'json',
}).done(function(resp){
  //here is problem
  //as initially no div is present
  //how can I load it with jquery
});

【问题讨论】:

    标签: javascript php jquery ajax


    【解决方案1】:

    你可以这样做,看起来不错。不要忘记查询的异步。所以你的代码将:

    $.ajax({
      url: 'layout.php',
      dataType: 'json',
    }).done(function(resp){
      $('#content').html(resp.layout);
    });
    
    <?php
       $app = new App;
       $ret = $app->header();
       $ret .= $app->sidebar();
       $ret .= $app->footer();
       echo json_encode({layout: $ret});
     ?>
    

    【讨论】:

    • $('#content') div 不存在。最初是空白页。
    • 然后使用 $('body'),或者创建它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 2012-01-19
    • 2017-09-10
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多