2011-09-30 18 views
0

使菜單消失後,我無法讓我的內容顯示,但未列出特定的html。如何從未指定的html中加載片段?

這裏是HTML:

<div id="header"> 
<h1>N300 Project Gallery</h1> 
</div> 
<div id="container"> 
<div id="utility"> 
<ul> 
    <li><a href="about.html">About</a></li> 
    <li><a href="projects.html">Projects</a></li> 

</ul> 

</div> 
    <div id="index-content">?</div> 
    <div id="footer">This is the footer</div> 
</div> 

這裏是我的腳本:

$(document).ready(function() { 

    $('#utility a').click(function(e){ 
     e.preventDefault(); 
     $('#utility').hide('normal',loadContent); 
    }); 
    function loadContent() { 
     $('#index-content').load(url+ '#content') 
    } 
}); 

這裏是片段我想移動:

<div id="content"> 
    <p>Hello! My name is Brittany Shephard.</p> 
</div> 

回答

0

注意,如果你想在fragment loading behavior+ ' #content'而不是+ '#content'),你需要在你傳遞給​​網址#content前一個額外的空間。

您的片段位於單獨的文件中,是否正確?

假設您的片段從projects.html #content返回,請嘗試:

function loadContent() { 
    $('#index-content').load($(this).attr('href')+ ' #content'); 
} 

JQuery docs for hidethis被設置爲回調隱藏的DOM節點。我假設你想從你隱藏的錨點加載URL,所以你需要從該節點獲取href屬性。

+0

看起來它不起作用,因爲隱藏的元素不是鏈接本身,但它是容器。 – mkriheli

0

哪裏是在loadContent()url來自(哪裏 ?

如果您需要將其從e.target href屬性中傳遞出來,請爲hide的回調以及將url傳遞給loadContent創建一個匿名函數。 Somtehing像:

$(document).ready(function() { 

    $('#utility a').click(function(e){ 
     e.preventDefault(); 
     $('#utility').hide('normal', function() { 
      loadContent($(e.target).attr('href')); 
     }); 
    }); 

    function loadContent(url) { 
     $('#index-content').load(url + ' #content'); 
    } 
}); 
+0

該網址是一個支持about.html,但我想加載一個不同的('page.html #content')基於點擊鏈接。你能寫出你在描述什麼嗎? – BrittanyNicole

+0

我已經更新了答案。 – mkriheli