jquery append external html file into my page

后端 未结 5 476
长发绾君心
长发绾君心 2020-12-02 14:48

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

$.get(\"banner.html\", function(data){
    $(this).chil         


        
相关标签:
5条回答
  • 2020-12-02 14:57
    <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>
    
    0 讨论(0)
  • 2020-12-02 15:09

    You can use jquery's load function here.

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

    If you need more info, here is the link.

    0 讨论(0)
  • 2020-12-02 15:11

    Use html instead of append:

    $.get("banner.html", function(data){
        $(this).children("div:first").html(data);
    });
    
    0 讨论(0)
  • 2020-12-02 15:15

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

    <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>
    
    0 讨论(0)
  • 2020-12-02 15:17

    Use selectors like CSS3

    $("banner.html>div:first-child").append(data);
    
    0 讨论(0)
提交回复
热议问题