我正在通过 AJAX 加载页面并将其放入 DIV 中。包括 HTML 标签等。似乎没有浏览器担心这一点,除了 <head>元素永远不会在 IE 中加载,但会在 FF 中加载。
是否可以让<head>中的元素也要加载,还是必须将它们放入正文中?
这包括<title>和<link> 。但是<style>元素工作正常。所以我无法使用 Ajax 加载外部样式表。有办法吗,还是必须把它放进体内?
提前致谢。
代码示例:
// page to load
<html>
<head>
<link href="{$cssLink}" type="text/css" rel="stylesheet"> // doesn't load
<style type="text/css">
/* CSS bla does work*/
</style>
</head>
<body>
<div id="testPage_content" class="content">
</div>
</body>
</html>
还有我正在进行的 ajax 调用,如果您在答案中需要的话。
// ask for the page
$.ajax(
{
url: "Scripts/loader.php",
type: "GET",
data: data, // created above this call
cache: false,
success: function (html)
{
//add the content retrieved from ajax and put it in the #content div
$('#content').html(html);
// only use fading if opacity is supported
if($.support.opacity)
{
$('#loading').hide();
$('#content').fadeIn();
}
}
});
请您参考如下方法:
你真的应该使用<iframe>元素来加载“完整”网站(意味着带有 <html>, <body> and <head> 标签。否则,这是 100% 无效的标记。
我的意思是脱掉衣服会是一件令人讨厌的工作,但是..也许没那么讨厌:
success: function (html)
{
//add the content retrieved from ajax and put it in the #content div
var stripped = $(html),
head = document.getElementsByTagName('head')[0] || document.documentElement;
stripped.find('head').contents().each(function(index, node) {
if(node.nodeType !== 3) {
node.setAttribute('data-insertID', 'foobar');
head.appendChild(node, head.firstChild);
}
});
$('#content').html(stripped.find('body').contents());
// only use fading if opacity is supported
if($.support.opacity)
{
$('#loading').hide();
$('#content').fadeIn();
}
}
删除:
$('head').find('[data-insertID]').remove();
这将从 head section 获取所有节点您收到的网站并将其放入您的实际网站中。之后,它从 <body> 获取所有节点。标记并将它们放入您的 div 中..应该可以工作,请告诉我:)
