简体   繁体   English

jQuery将html文件加载到<div>

[英]jQuery load html file into <div>

I wanted to know how I can use an html file and use it to replace the space inside a 我想知道我怎么可以用一个HTML文件,并用它来取代内部的空间

I know you can do it with jquery but for some reason it isn't working out for me. 我知道你可以用jQuery做,但由于某种原因,它不工作了我。 I have: 我有:

$(document).ready(function() {    
    $('#nav li a').click(function(){  
        var toLoad = $(this).attr('href');  
        $('#main').hide('fast',loadContent);   
        function loadContent() {  
            $('#main').load(toLoad);  
        }  
        return false; 
    });  
});  

So whenever I click a link from my navbar it should edit the #main div and bring up the corresponding html file to replace the div with. 因此,每当我单击导航栏中的链接时,都应编辑#main div并调出相应的html文件来替换div。 For some reason it just leaves it blank. 由于某种原因,它只是空白。

Check this line 检查这条线

 $('#main').hide('fast',loadContent); 

It is hiding the div where you are loading the content. 它隐藏了您正在加载内容的div。 If you show that div again, you can see the content on div. 如果再次显示该div,则可以在div上看到内容。 So 所以

 Change 

 $('#main').load(toLoad);

 to 

 $('#main').load(toLoad).show();

You may try this 你可以试试这个

$(document).ready(function(){ 
    $('#nav li a').click(function(e){  
        e.preventDefault();
        var toLoad = $(this).attr('href');
        $('#main').hide('fast', function(){  
            $('#main').load(toLoad, function(){
                $('#main').show('fast');
            });  
        });
    });
});

$('#main').hide('fast',loadContent); is hiding the div so need to show it again so I've used this $('#main').show('fast'); 正在隐藏div因此需要再次显示它,所以我使用了$('#main').show('fast'); in load's callback function. 在加载的回调函数中。

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

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