页面中如何引用外部的HTML(四种方法)
生活随笔
收集整理的這篇文章主要介紹了
页面中如何引用外部的HTML(四种方法)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
頁面中如何引用外部的HTML(四種方法)
一、總結(jié)
一句話總結(jié):a、iframe標(biāo)簽??????? b、ajax引入代碼片段??????? c、link import的方法導(dǎo)入??????? d、requirejs
?
1、引入外部文件的四種方法?
a、iframe標(biāo)簽
b、ajax引入代碼片段
c、link import的方法導(dǎo)入
d、requirejs
?
?
二、如何在頁面引用外部的HTML?
轉(zhuǎn)載 https://segmentfault.com/a/1190000010943143
在頁面內(nèi)部應(yīng)用外部的html片段,是一個很常見的用法,那么有哪些方法可以實現(xiàn)呢?
根據(jù)情況使用不同的方法。
iframe
如果是一個完整的外部頁面(有完整的html,head,body),可以考慮用iframe。
<iframe src="test.html"></iframe>ajax
如果僅僅是一個代碼片段,也可以用ajax
$.get('test.html',function(html){// }); $.when($.get('test1.html'),$.get('test2.html'), ).then(function(html1,html2){console.log(html1[0]);console.log(html2[0]); });link import的方法導(dǎo)入
<link rel="import" href="test.html">if (typeof document.querySelector("link[rel = 'import']").import == 'object') {var html = document.querySelector("link[rel = 'import']").import.head.innerHTML; } else {$.get('test.html', function(html) {}); }requirejs
最后,也可以使用requirejs…(比ajax方便)
轉(zhuǎn)載于:https://www.cnblogs.com/Renyi-Fan/p/9284231.html
總結(jié)
以上是生活随笔為你收集整理的页面中如何引用外部的HTML(四种方法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个浏览器插件,测试xpath的工具
- 下一篇: Promise 化回调式函数