简体   繁体   English

使用Ajax JavaScript将内容加载到网页中的div中

[英]Using ajax javascript to load content into a div in web page

I am having a little problem using javascript-ajax here. 我在这里使用javascript-ajax时遇到了一些问题。 In my page, I load in the content into one of the div with id content in an ajax manner, whenever the user clicks on links which have the class myajaxreq , and the contents are loaded into the div in a fade in manner. 在我的页面中,每当用户单击具有myajaxreq类的链接时,我就会以ajax的方式将内容加载到具有id content的div之一中,并且内容以淡入淡出的方式加载到div中。 The javascript that I am using is this 我正在使用的JavaScript是

$(document).ready(function(){
    $("#content").load($('.myajaxreq:first').attr('href'));
});


$('.myajaxreq').click(function() {
    var myhref=$(this).attr('href');
    $('#content').hide().load(myhref).fadeIn('slow');

    return false;
}); 

All works great on localhost , but when i put it online and then when we click on these links, then: First the same content which was initially there in the div is loaded in fade in manner. 所有这些都可以在localhost很好地工作,但是当我将其放到网上,然后单击这些链接时,则:首先,淡入淡出地加载div中最初存在的相同内容。 After a few seconds, the new content is loaded. 几秒钟后,将加载新内容。

I think I am missing some sort of 我想我缺少某种

if(content document is ready)
     then load in a fade in manner
         and so on..

Please somebody help me out here !! 请有人在这里帮助我!

call fade in after success callback... try this 成功回调后调用淡入...尝试一下

var jContent = $('#content').hide();
jContent.load(
        myhref,
        {},
        function(){
            jContent.fadeIn('slow');
        }
    );

here the whole code (untested) 这里的整个代码(未经测试)

$(document).ready(function(){
    var jContent = $("#content").load($('.myajaxreq:first').attr('href'));

    $('.myajaxreq').click(function() {
        var myhref=$(this).attr('href');
        jContent
          .hide()
          .load(
            myhref,
            {},
            function(){
                jContent.fadeIn('slow');
            }
        );

        return false;
    }); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM