.load无法在IE8中运行

时间:2012-12-21 17:16:23

标签: javascript jquery html internet-explorer-8 load

过去几天我一直在研究这个问题,但无法弄明白。下面的代码在外部文件中搜索基于当前页面类的内容,然后将内容加载到页面上任何匹配的ID中。它适用于Chrome,Firefox,IE9,但最近停止在IE8中工作,我无法弄清楚原因。任何想法都会非常感激。

HTML看起来像这样

<body class="jms">
    <div id="mainHomeContent" class="shared"></div>
</body>

jquery在准备好的

上运行
$("div.shared").each(function(){
        var Body = $(document).find("body");
        var contentID = ("#" + $(this).attr("id"));
        var pathname = ""

        if(Body.hasClass("pigman")){
            var pathname = "/dev/jmsracing/content/pigman/shared-content-include.html"
        } else if(Body.hasClass("marion-arts")){
            var pathname = "/dev/jmsracing/content/marion-arts/shared-content-include.html"
        } else if(Body.hasClass("jms")){
            var pathname = "/dev/jmsracing/content/jms/shared-content-include.html"
            alert('hello');
        }

        $(contentID).load(pathname + " " + contentID);  
    }); 

2 个答案:

答案 0 :(得分:2)

我认为他正在使用id进行迭代,其中ie非常严格,所以这应该是解决方案:

$(function() {
    var Body = $(document).find("body");
    var contentID = ("#" + $(this).attr("id"));
    var pathname = ""

    if (Body.hasClass("pigman")) {
        var pathname = "/dev/jmsracing/content/pigman/shared-content-include.html"
    } else if (Body.hasClass("marion-arts")) {
        var pathname = "/dev/jmsracing/content/marion-arts/shared-content-include.html"
    } else if (Body.hasClass("jms")) {
        var pathname = "/dev/jmsracing/content/jms/shared-content-include.html"
        alert('hello');
    }

    $(contentID).load(pathname + " " + contentID);
});​

答案 1 :(得分:1)

试试这个:

$("div.shared").each(function () {
    //combined into one var statement...not really necessary.
    var $body = $("body"),
        contentId = "#" + $(this).attr("id"),
        pathname = "";

    //you've declared pathname above no need for "var" each time below
    //also added missing semi colons
    if ($body.hasClass("pigman")) {
        pathname = "/dev/jmsracing/content/pigman/shared-content-include.html";
    } else if ($body.hasClass("marion-arts")) {
        pathname = "/dev/jmsracing/content/marion-arts/shared-content-include.html";
    } else if ($body.hasClass("jms")) {
        pathname = "/dev/jmsracing/content/jms/shared-content-include.html";
        alert('hello');
    }
    // $(this) and $(contentId) are the same element 
    // since you are getting the "id" from "this"
    // us $(this) instead
    $(this).load(pathname + " " + contentId);  
});