【问题标题】:How to load external html into a div?如何将外部 html 加载到 div 中?
【发布时间】:2013-10-15 06:55:17
【问题描述】:

我已经使用 jquery 创建了这个小代码来将外部 HTML 文件加载到我的 div 中,但是它不起作用,我的想法已经用完了。任何其他 jquery 代码都可以正常工作。任何帮助表示赞赏:

<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 html load external


【解决方案1】:

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

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

【讨论】:

  • 谢谢,但还是不行。这是 JS 控制台给我的:Access-Control-Allow-Origin 不允许 Origin null。
  • @user2660811 您可能正在通过双击脚本来执行它。尝试使用 localhost 或类似的方式通过 Web 服务器调用它。
【解决方案2】:
<script type="text/javascript"> 
    $('#zaladuj').click(function () { 
        $.ajax({
            context: this,
            dataType : "html",
            url : "s1.htm",
            success : function(results) {
                $(this).html(results);
            }
        });
    }); 
</script> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    相关资源
    最近更新 更多