2016-04-22 46 views
0

我試圖使用jQuery在單擊導航鏈接時改變我的主頁的主要內容區域。我使用.load()來做到這一點,但我引用的外部html頁面不會加載(沒有任何反應)。jQuery將不會加載外部HTML模板()

custom.js

$(document).ready(function(){ 
    $("#about").click(function(event){ 
    event.preventDefault(); 
     $(".main-content").fadeToggle("slow", function(){ 
     $(this).load("about-us.html") 
     }); 
    }); 
}); 

的index.html

<div class="inner cover main-content"> 
    <h1 class="cover-heading">A heading</h1> 
    <p class="lead">This is the best page of all time.</p> 
</div> 

回答

1

想我會回答我的問題,因爲它花費幾個小時才能弄清楚的東西那麼微不足道。

如果你加載本地的jQuery,您必須使用像MAMP而不是它只是「點擊」(例如加載在瀏覽器中的文件)的本地Web服務器運行該文件。

如果jQuery不從某種Web服務器提供服務,jQuery將不會加載外部文件。

如果您檢查控制檯,你會看到,上面寫着類似的錯誤:

MLHttpRequest cannot load file:///Users/yourname/filepath/index.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. 
+0

爲什麼不接受你自己的答案。大聲笑 – Velocibadgery

+0

@Aaron它不會讓我48小時。不幸的是,我也無法提出自己的答案;) – msarlo