【问题标题】:IE doesn't load css for .load() elements & js doesn't workIE 不为 .load() 元素加载 css & js 不起作用
【发布时间】:2011-09-07 16:31:40
【问题描述】:

我用 .load() 这个函数加载我的内容:

$(document).ready(function(){
    $("nav a").click(function () {  
    $("#main-content").load($(this).attr("href") + " #content > *");
    return false;
   });
});

index.php 包含样式和脚本。

加载的内容只包含 div 中的内容,没有头部、主体或样式。

问题:

- IE8 不加载样式并且此功能不起作用:

$(document).ready(function() {

   if ($(window).width() < 630) {
        $('aside').each(
            function(){
                $(this).parents('article').find('h2').after(this);
                    }
        );  
        }

   if ($(window).width() > 630) {
        $('aside').each(
            function(){
                $(this).parents('section').after(this);
                    }
        );  
        }
});


$(window).bind('resize', function(){

   if ($(window).width() < 630) {
        $('aside').each(
            function(){
                $(this).parents('article').find('h2').after(this);
                    }
        );  
        }

   if ($(window).width() > 630) {
        $('aside').each(
            function(){
                $(this).parents('section').after(this);
                    }
        );  
        }

}); 

我已经尝试像其他脚本一样修复它:

$(document).ajaxComplete(function(){ });

..但它没有工作。

非常感谢任何帮助。

【问题讨论】:

    标签: jquery css internet-explorer


    【解决方案1】:

    使用&lt;link&gt; 显式加载CSS,并使用$.getScript() 加载和执行javaScript 文件。

    【讨论】:

    • css 已经链接在 index.php 的头部。 getscript() 不能解决问题。
    • 您正在使用$("#main-content").load($(this).attr("href") + " #content &gt; *"),它只会加载与该选择器匹配的 DOM 元素。它不会抓取 CSS(除非您使用内联样式)。
    【解决方案2】:

    aside 是一个类吗?

    那么您将不得不使用
    $('.aside').each( 而不是
    $('aside').each(

    articlesection 也是如此。

    【讨论】:

      【解决方案3】:

      我发现了问题:这是因为我在页面上使用了 HTML5 元素。使用 HTML5shiv 让它们通常在页面上运行是没有问题的,但 IE 不会通过 .load() 在加载的内容上捕获它们。

      这里已经有一个关于这个的话题了。

      How to "enable" HTML5 elements in IE 8 that were inserted by AJAX call?

      innershiv 已经是一个解决方案:

      http://css-tricks.com/6869-html5-innershiv/

      编辑:但命名函数仍有问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-01
        • 1970-01-01
        • 2013-04-01
        • 2013-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-14
        相关资源
        最近更新 更多