使用jQuery加载内容而不刷新

时间:2011-07-21 06:45:57

标签: javascript jquery

我想用jQuery加载内容(是html)。为什么以下代码对我不起作用? 我使用本教程:http://net.tutsplus.com/tutorials/javascript-ajax/how-to-load-in-and-animate-content-with-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;

});

1 个答案:

答案 0 :(得分:0)

首先:

如果您希望正确回答问题,则应尽可能多地提供相关信息。

其次,快速查看代码:

您需要在回调函数时传递对函数的引用,而不是调用函数(showNewContent()而不是showNewContent),并传递它返回的任何内容(在您的情况下,没有)

请改为尝试:

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

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