jquery ajax没有在IE中的<head>标签中添加<link>

Mar*_*nix 5 jquery

我正在通过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>
Run Code Online (Sandbox Code Playgroud)

如果你在答案中需要那个,我正在制作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();
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

jAn*_*ndy 3

你真的应该使用一个<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();
    }
}
Run Code Online (Sandbox Code Playgroud)

删除:

$('head').find('[data-insertID]').remove();
Run Code Online (Sandbox Code Playgroud)

这将从您收到的网站中获取所有节点head section并将其放入您的实际网站中。之后,它从<body>标签中获取所有节点并将它们放入您的 div 中..应该可以工作,请告诉我:)