[英]Load text into div using jQuery when the div was created by jQuery's .load() function
我目前正在制作一個網站,該網站能夠加載模板以使其外觀使用,並加載包含將放置在post div中的文本的“ post”文檔。 但是我遇到了一個小障礙。
看來jQuery無法看到使用.load函數創建的div
當通過jQuery的.load()函數創建div時,我需要解決將文本加載到div中的問題。
HTML代碼
<body>
<div id="header">
</div>
<script>
$( "#header" ).load( "/Templates/Template.html #header" );
$( "#post" ).load( "/post.html" );
</script>
</body>
Template.html代碼
<body>
<div id="header">
<div id="post">
</div>
</div>
</body>
Post.html代碼
<p>This is the text that will be displayed in the post</p>
我將如何使jQuery以“ post”作為id來查看div。 如果這樣會更容易,則可以使用PHP
由於ajax是異步的,並且這兩個.load()
運行,因此需要使用on load complete函數,因此調用div時#post
尚不存在。
嘗試這個:
$("#header").load("/Templates/Template.html #header", function () {
$("#post").load("/post.html");
});
並確保在Template.html
#header
有一個#post
,因為您要替換的divs內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.