2013-06-28 64 views
3

所以我看到大量的人問如何加載HTML到一個div,我的代碼做得很好....但隨後頁面的其餘部分改變,當我將html加載到div中。加載html到div而不改變頁面的其餘部分

我有一個頁面a.html看起來像這樣,和負載b.html

a.html

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
$(function() { 
$("#includedContent").load("b.html"); 
}); 
</script> 



<div id="includedContent"></div> 
<h1>This is why I rule</h1> 
</head> 
</html> 

然後b.html看起來像這樣

<p> This is my include file </p>

會發生什麼是a.html加載,我可以立即看到This is why I rule,但隨後代碼熄滅並獲取b.html。當b.html加載時,我只能看到This is my include file和'這就是我爲什麼規則'的消息消失...

這是爲什麼?我如何防止它?它在我測試過的所有瀏覽器上執行此操作。

+0

你的HTML是無效的。 'head'標籤內不允許使用'div'和'h1'標籤。 – Alan

+3

將您的標記從您的''元素中取出並放入正文中。 –

回答

10

你必須寫你的HTML代碼放到一個<body> </body>

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
$(function() { 
$("#includedContent").load("b.html"); 
}); 
</script> 
</head> 

<body> 
<div id="includedContent"></div> 
<h1>This is why I rule</h1> 
</body> 
</html> 
3

你所有的HTML標記是在head部分不是body

試試這個:

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

<script> 
$(function() { 
$("#includedContent").load("b.html"); 
}); 
</script> 

</head> 
<body> 
    <div id="includedContent"></div> 
    <h1>This is why I rule</h1> 
</body> 
</html> 
+0

我添加了body標籤,但它沒有幫助。 – Rell3oT

+0

啊哈。它確實有用!我的錯。謝謝您的幫助。這樣簡單的修復! – Rell3oT

+0

不用擔心隊友:) –

相關問題