jQuery 加载 HTML 到 <div>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11130586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery load HTML into <div>
提问by Papa De Beau
Is there a way with jQuery to load Html contents from a url?
jQuery 有没有办法从 url 加载 Html 内容?
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?
我可以用 URL 中的内容替换 div 中的所有 HTML 吗?
回答by SomeKittens
回答by Bishnu Paudel
Firstly, create an HTML page with this code
首先,使用此代码创建一个 HTML 页面
<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.
在 DIV 中有一个 iFrame(可选)并使用 jQuery 将 iFrame 的源更改为 URL。该 url 应指向您在上面创建的页面。
function loadIframe(iframeName, url) {
var $iframe = $('#' + iframeName);
if ( $iframe.length ) {
$iframe.attr('src',url);
return false;
}
return true;
}
回答by seyed
using .get()
and .html()
in jQuery. ex,
在 jQuery 中使用.get()
和.html()
。前任,
$.get(url, function (data) {
$('.MyContent').html(data);
});