jQuery load HTML into

2019-02-25 05:44发布

Is there a way with jQuery to load Html contents from a url?

For example:

<div class="MyContent">

<html>
<head>
<title>Hello World Page</title>
</head>
<body>
Hello World
</body>
</html>

</div>

Can I replace all of the HTML above in the div with the content from a URL?

标签: jquery url html
3条回答
戒情不戒烟
2楼-- · 2019-02-25 05:53

You're looking for .load()

Load data from the server and place the returned HTML into the matched element.

查看更多
走好不送
3楼-- · 2019-02-25 06:08

Firstly, create an HTML page with this code

<html>
<head>
<title>Hello World Page</title>
</head>
<body>
Hello World
</body>
</html>

Have an iFrame inside a DIV (optional) and change the source of the iFrame to URL using jQuery. The url should point to the page you create above.

function loadIframe(iframeName, url) {
    var $iframe = $('#' + iframeName);
    if ( $iframe.length ) {
        $iframe.attr('src',url);   
        return false;
    }
    return true;
}
查看更多
▲ chillily
4楼-- · 2019-02-25 06:17

using .get() and .html() in jQuery. ex,

$.get(url, function (data) {
   $('.MyContent').html(data);
});
查看更多
登录 后发表回答