jQuery load HTML into

2019-02-25 05:43发布

问题:

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?

回答1:

You're looking for .load()

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



回答2:

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;
}


回答3:

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

$.get(url, function (data) {
   $('.MyContent').html(data);
});


标签: jquery url html