jQuery内容首先加载

时间:2014-12-10 14:27:29

标签: jquery

我有一个div:

 内容

我制作了一个jQuery,它会将url的名称更改为带有hash的内容...等等。我的问题是当我进入页面example.com/#/page-1时,它首先从.content加载内容几毫秒然后它改变我的内容。 如何首先加载内容?我必须在加载之前清空.content吗?

感谢。

$(function() {

    var $mainContent = $('.content'),
        siteUrl = "http://" + top.location.host.toString(),
        $internalLinks = $('a[href^="' + siteUrl + '"]'),
        hash = window.location.hash,
        URL = '',
        $el;

    if (hash) {
        hash = hash.substring(1);
        URL = hash + '#inside';
        $mainContent.load(URL);
    };

    $internalLinks.each(function(){
        $(this).attr("href", "#" + this.pathname);
    }).click(function(){
        load_posts();
        $el = $(this);
        URL = $el.attr("href").substring(1);
        URL = URL + '#inside';

    });

});

2 个答案:

答案 0 :(得分:0)

正如迈克已经在这里说的那样,最好的方法是默认隐藏你的html中的所有正文部分。

<body style="display:none;">

然后在脚本的末尾添加以下行:

$('Body').show();

但要小心。这可能会导致javascript关闭的用户认为该网站崩溃的问题。

答案 1 :(得分:0)

好的......我用这个解决了我的问题:

$( document ).ready(function() {
   $('.content').empty();
});

我的代码出现问题。当我将内容加载到.content中时,我有类似的内容,所以我不能像你们所说的那样隐藏它,因为它一遍又一遍地重复同样的事情。你的答案是对的。谢谢。

相关问题