简体   繁体   English

使用html或jquery将外部页面加载到div中

[英]load external page into a div with html or jquery

So, I have this code in index.html: 因此,我在index.html中有以下代码:

<td><a id="link" href="">Vídeos</a></td>

and then 接着

<div name="content" id="content"></div>

I want to load a page named vid_content.html 我想加载一个名为vid_content.html的页面

I tried a lot of code I found everywhere but nothing went right.. 我尝试了很多到处都可以找到的代码,但是没有正确的方法..

PS: all the pages are local in the same folder! PS:所有页面都在同一文件夹中!

The last thing I tried was: 我尝试的最后一件事是:

$(document).ready(function(){ 
    $("#link").click(function(){ 
        $("#content").load("vid_content.html");
    });
}

用这个

 $('#content').load('/path/to/vid_content.html');

You should be able to make use of .html() within jQuery to define what the html contained within the div is. 您应该能够在jQuery中使用.html()来定义div中包含的html是什么。

    $(document).ready(function(){ 
      $("#link").click(function(){ 
        $("#content").html("vid_content.html");
      });
    }

There's a few problems with the code. 代码有一些问题。 Firstly, the Ajax request won't work unless you're running this on a local server. 首先,除非您在本地服务器上运行,否则Ajax请求将无法正常工作。 Simply opening the HTML file in Chrome won't work. 仅仅在Chrome中打开HTML文件是行不通的。 Secondly, you need to pass in the event object to the click handler so that you can stop the page reloading when you click on the link, like so: 其次,您需要将事件对象传递给click处理程序,以便单击链接时可以停止页面重新加载,如下所示:

$(document).ready(function(){ 
    $("#link").click(function(e){ 
        $("#content").load("vid_content.html");
        e.preventDefault();
    });
});

Lastly, you were missing the final closing bracket from the jQuery ready function. 最后,您缺少jQuery ready函数的最后一个括弧。

Already got a solution! 已经有解决方案了!

Done it this way: 这样做:

function load_vids(){ document.getElementById("content").innerHTML='<object type="text/html" data="vid_content.html #content" style="width:811px; height:570px"></object>'; }

And this: 和这个:

<td><a id="link" href="#" onclick="load_vids()">Videos</a></td>

Thank you all for help! 谢谢大家的帮助!

Save this into "firstpage.php". 将其保存到“ firstpage.php”。 with -> jQuery v2.2.3 与-> jQuery v2.2.3

$(document).ready(function() {

    $('#content').load("secondPage.php", function() {
        //do something
    });

    $(document).on('click','#third',function(){
        $('#content').load("thirdPage.php", function() {
            //do something
        });
    });

});

Into secondPage.php 进入secondPage.php

page 2<br>
----------------------<br>
<a href="#" id="third">load third</a>

Into thirdPage.php 进入thirdPage.php

hello world

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

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