如何将外部html加载到一个div? [英] How to load external html into a div?

查看:144
本文介绍了如何将外部html加载到一个div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jquery创建了这个小代码,将一个外部HTML文件加载到我的div中,但它不起作用,我正在用尽想法。任何其他jQuery代码运作良好。感谢任何帮助:

I've created this little code using jquery to load an external HTML file into my div, but it doesn't work, I'm running out of ideas. Any other jquery code works well. Any help appreciated:

<div id="zaladuj"> 
load external html file
</div> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
    $('#zaladuj').click(function () { 
        $(this).load('s1.htm'); 
    }); 
</script> 


推荐答案

您需要将代码封装在 jQuery.ready()函数,因为您必须等待DOM完全加载。

You need to wrap your code within jQuery.ready() function since you have to wait for DOM to be fully loaded.

<script type="text/javascript">
    $(document).ready(function(){
        $('#zaladuj').click(function () { 
            $(this).load('s1.htm'); 
        }); 
    });
</script>

这篇关于如何将外部html加载到一个div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆