2011-07-21 94 views
0

我想用jQuery加載內容(是html)。爲什麼下面的代碼不適合我?
我使用本教程:http://net.tutsplus.com/tutorials/javascript-ajax/how-to-load-in-and-animate-content-with-jquery/
用jQuery加載內容不刷新

HTML:
點擊每個此鏈路負載頁它(HREF)的。

<div id="icon"> 
    <a href="http://localhost/test/test2.php" id="delete_icon"></a> 
    <a href="<?=base_url();?>admin/tour/insert_foreign" id="add_icon" ></a> 
    <a href="http://localhost/test/1st.htm" id="edit_icon" ></a> 
    <a href="http://localhost/test/index1.php" id="print_icon"></a> 
    </div> 

JS:

var hash = window.location.hash.substr(1); 
var href = $('#nav li a').each(function() { 
    var href = $(this).attr('href'); 
    if (hash == href.substr(0, href.length - 5)) { 
     var toLoad = hash + '.html #content'; 
     $('#content').load(toLoad) 
    } 
}); 

$('#icon a').click(function() { 

    var toLoad = $(this).attr('href') + ' #content'; 
    $('#content').hide('fast', loadContent); 
    $('#load').remove(); 
    $('#wrapper').append('<span id="load">LOADING...</span>'); 
    $('#load').fadeIn('normal'); 
    window.location.hash = $(this).attr('href').substr(0, $(this).attr('href').length - 5); 

    function loadContent() { 
     $('#content').load(toLoad, '', showNewContent()) 
    } 

    function showNewContent() { 
     $('#content').show('normal', hideLoader()); 
    } 

    function hideLoader() { 
     $('#load').fadeOut('normal'); 
    } 
    return false; 

}); 
+0

你試圖把'event'在點擊功能:'函數(事件){...'然後做了'event.preventDefualt()'? –

+0

op寫了'return false; ''和'event.preventdefault'一樣 – Vivek

回答

0

首先:

如果你想你的問題回答正確,你應該有儘可能多的相關信息成爲可能。

其次,望着迅速代碼:

您需要將引用傳遞給函數,你回調函數,而不是要調用的函數(的showNewContent()代替showNewContent),並通過一切可能返回(在你的情況,沒有)。

試試這個:

function loadContent() { 
    $('#content').load(toLoad, '', showNewContent); 
} 

function showNewContent() { 
    $('#content').show('normal', hideLoader); 
}