【问题标题】:Trying to load a table into DIV from HTML file试图将表格从 HTML 文件加载到 DIV
【发布时间】:2015-11-18 14:09:13
【问题描述】:

我正在尝试将 .html 文件的全部内容加载到我在另一个页面中使用 Javascript/JQuery 的 div 中,并且目前有:

$( "#divToFill" ).load("newContent.html");

但是,这会将 DIV 中的现有内容替换为空内容,并在屏幕上留下空白空间。 newContent.html中的内容如下:

 <table cellspacing="0" cellpadding="3" border="0" class="results" id="results">
    <thead id="tableHeader">
       <tr class="column-headers">
          <td width="15%">1</td>
          <td width="10%">2</td>
          <td width="35%">3</td>
          <td width="17%">4</td>
          <td width="5%">5</td>
          <td width="18%">6</td>
       </tr>
    </thead>
    <tbody>
       <tr class="row">
          <td class="column">
             <div style="word-wrap:break-word;overflow:auto">CONTENT</div>
          </td>
          <td class="2">Num5757474747</td>
          <td class="3">
             <span>
                <div class="divStyle2"> TEXT TEXT TEXT </div>
             </span>
          </td>
          <td class="4">HELLO</td>
          <td class="5">10:00</td>
          <td class="6">To be done</td>
       </tr>
    </tbody>
 </table>

有什么帮助吗?

【问题讨论】:

  • 你可以试着把这个放在文档里面: $(document).ready(function() { $( "#divToFill" ).load("newContent.html"); });跨度>
  • 您是否收到任何控制台错误?
  • 我已尝试按照@PauloMenezes 的建议使用准备好的文档,但这对结果没有影响。
  • @KayVan 没有要报告的控制台错误。我希望有,可能会更容易解决。
  • 试试 $( "#divToFill" ).load("newContent.html #results");

标签: jquery html ajax load


【解决方案1】:

你可以尝试通过php文件加载它

<script>
$( "#success" ).load( "/not-here.php", function( response, status, xhr ) {
  if ( status == "error" ) {
    var msg = "Sorry but there was an error: ";
    $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
  }
});
</script>

如果不试试这个

<script type="text/javascript"> 
            var includeDiv = $("#include");
            $(window).on('hashchange', function() {
                var href = location.hash.slice(1) +".html";
                includeDiv.load('include/' + href);
            });
 </script>

【讨论】:

    【解决方案2】:

    jQuery.load() 函数需要页面的整个 URI。因此,如果您在实时网站上进行开发,则需要......

    $("#DivToFill").load("http://domain/directory/fileName.html");
    

    另外,尝试在控制台中输出加载函数的响应。这将帮助您进一步调试。你这样做的方式是......

            $("#DivToFill").load("http://domain/directory/fileName.html",function(response, status, xhr ){
             console.log(xhr);
             console.log(response);
             console.log(status);
            }
    

    希望这会有所帮助...

    【讨论】:

    • 感谢您的回复。但是,我不认为这是问题所在。如果我将 .html 文件的内容更改为“testing”这个词,它会显示得很好......表明文件正在加载,但在文件加载和放入 DIV 之间的某个位置,表格无法显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多