2012-12-21 114 views
1

過去幾天我一直在調查這個問題,無法弄清楚。下面的代碼根據當前頁面類搜索外部文件中的內容,然後將內容加載到頁面上的任何匹配的ID中。它適用於Chrome,Firefox,IE9,但最近停止在IE8中工作,我無法弄清楚爲什麼。任何想法將不勝感激。.load在IE8中不工作

HTML看起來像準備

$("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

你錯過了'var pathname =「」'是你運行的代碼中的錯誤? – KTastrophy

+1

控制檯中是否存在錯誤? Ajax請求是否正確? – epascarello

+0

可能會有這幫助:http://stackoverflow.com/questions/1061525/jquerys-load-not-working-in-ie-but-fine-in-firefox-chrome-and-safari – Jai

回答

1

運行此

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

的jQuery試試這個:

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

這與OP的代碼有何不同。 – Jai

+0

@Jai - 他沒有重定義路徑名4次,沒有使用低效的身體選擇器並正確地通過jslint –

+0

所以它主要只是清理。謝謝您的幫助。兩者都是很好的答案。另外我更改了 $(this).load(pathname +「」+ contentId +「Content」); 所以加載的div不會有相同的名稱。再次感謝 – trobbins26

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