Include file In HTML by JavaScript or jquery dynamically

前端 未结 1 1610
既然无缘
既然无缘 2021-01-17 06:12

I tried the following code. But it doesn\'t work properly. Only one file add/ include repetitively. How can I solve this? **I want to create a custom tag \"\" which can INCL

相关标签:
1条回答
  • 2021-01-17 06:40

    <include> is not a valid html element.

    You can use <link> element with rel attribute set to "import" to load resources into document. At load event of <link> element you can get the content of resource using link.import

    <link rel="import" href="nav.html" type="text/html">
    

       $(function() {
         $("body").append($("link[href='nav.html']")[0].import.body.innerHTML)
       })
    

       $("link[href='nav.html']").on("load", function() {
         $("body").append(this.import.body.innerHTML)
       })
    

    Alternatively, you can use .load()

       $("#element").load("nav.html");
    
    0 讨论(0)
提交回复
热议问题