【问题标题】:jquery append external html file into my pagejquery 将外部 html 文件附加到我的页面中
【发布时间】:2012-01-06 19:51:04
【问题描述】:

我正在尝试以这种方式附加一个外部 html 内容(div 加上一些文本,用于测试):

$.get("banner.html", function(data){
    $(this).children("div:first").append(data);
});

这似乎很简单......但它似乎不起作用。任何想法?谢谢!

【问题讨论】:

  • 你的$(this) 是什么?你可以试试jquery load,而不是get

标签: jquery


【解决方案1】:

使用 html 代替追加:

$.get("banner.html", function(data){
    $(this).children("div:first").html(data);
});

【讨论】:

    【解决方案2】:

    我不确定您希望 this 在您的示例中引用什么。这是另一种方法:

    <html>
        <head>
            <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(function () {
                    $.get("banner.html", function (data) {
                        $("#appendToThis").append(data);
                    });
                });
            </script>
        </head>
        <body>
            <div id="appendToThis"></div>
        </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      你可以在这里使用jquery的加载功能。

      $("#your_element_id").load("file_name.html");
      

      如果您需要更多信息,here is the link

      【讨论】:

        【解决方案4】:
        <html>
            <head>
                <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
                <script type="text/javascript">
                    $(function () {
                        $.get("banner.html", function (data) {
                            $("#appendToThis").append(data);
                        });
                    });
                </script>
            </head>
            <body>
                <div id="appendToThis"></div>
            </body>
        </html>
        

        【讨论】:

          【解决方案5】:

          使用 CSS3 之类的选择器

          $("banner.html>div:first-child").append(data);
          

          【讨论】:

            猜你喜欢
            • 2016-03-28
            • 2017-12-03
            • 1970-01-01
            • 2012-07-31
            • 1970-01-01
            • 2015-03-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多