我有一个这样的网页:
<html>
<head>
. . .
</head>
<body>
<div id="wrapper">
<p>Lots of content here!</p>
</div>
</body>
</html>我还有一个像这样的外部文件:
<div id="more-stuff"><p>Even more content!</p></div>我想要的是有一个这样的网页:
<html>
<head>
. . .
</head>
<body>
<div id="wrapper">
<p>Lots of content here!</p>
<div id="more-stuff"><p>Even more content!</p></div>
</div>
</body>
</html>使用jQuery。我的猜测是这样的:
$(document).ready(function(){
$('#wrapper').append.load('/external.htm');
});但它不会工作,我似乎找不到一个好的解决方案。
发布于 2011-02-28 09:51:32
尝试如下所示:
$(document).ready(function(){
$.get('/external.htm', function(data) {
$('#wrapper').append(data);
});
});它告诉jQuery请求html文件,然后在准备就绪时运行回调(该回调会追加请求返回的数据)。
发布于 2011-02-28 09:55:52
.append()不是这样工作的。它需要附加文本。但是.load()会覆盖其目标的内容,因此您需要首先追加一个子级,然后加载到该子级。
$(document).ready(function(){
$('#wrapper').append($(document.createElement("p")).load('extern.html'));
});https://stackoverflow.com/questions/5137468
复制相似问题