jquery 将外部 html 文件附加到我的页面中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8277462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:16:23  来源:igfitidea点击:

jquery append external html file into my page

jquery

提问by xus

I'm trying to append an external html content (div plus some text inside just for testing) this way:

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

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

That seems to be pretty simple... but it does not seem to work. Any idea? Thanks!

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

回答by erimerturk

Use html instead of append:

使用 html 而不是 append:

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

回答by Lloyd

i'm not sure what you're expecting thisto refer to in your example.. here's an alternative method:

我不确定您希望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>

回答by Chamin Wickramarathna

You can use jquery's load function here.

您可以在此处使用 jquery 的加载功能。

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

If you need more info, here is the link.

如果您需要更多信息,这里是链接

回答by qwertyqq

<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>

回答by Hamid Reza Yazdani

Use selectors like CSS3

使用像 CSS3 这样的选择器

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