简体   繁体   English

.load无法在IE8中运行

[英].load not working in IE8

I have been looking into this issue for the past few days and cannot figure it out. 过去几天我一直在研究这个问题,无法弄明白。 The code below, searches an external file for content based off current page class, then loads content into any matching ID's on the page. 下面的代码在外部文件中搜索基于当前页面类的内容,然后将内容加载到页面上任何匹配的ID中。 It works in Chrome, Firefox, IE9 but recently stopped working in IE8 and I cannot figure out why. 它适用于Chrome,Firefox,IE9,但最近停止在IE8中工作,我无法弄清楚原因。 Any thoughts would be much appreciated. 任何想法将不胜感激。

HTML looks like this HTML看起来像这样

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

jquery running on ready 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);  
    }); 

What i think is he is iterating with same id where ie is very strict about it so this should be the solution: 我认为他正在迭代相同的idie非常严格,所以这应该是解决方案:

$(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);
});​

Try this: 尝试这个:

$("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);  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM