1

过去几天我一直在研究这个问题,但无法弄清楚。下面的代码根据当前页面类在外部文件中搜索内容,然后将内容加载到页面上任何匹配的 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);  
    }); 
4

2 回答 2

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);
});​
于 2012-12-21T18:00:54.153 回答
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);  
});
于 2012-12-21T17:48:03.857 回答