在頁面內部應用外部的html片斷,是一個很常見的用法,那麼有哪些方法能夠實現呢?html
根據狀況使用不一樣的方法。ajax
若是是一個完整的外部頁面(有完整的html,head,body),能夠考慮用iframe。requirejs
<iframe src="test.html"></iframe>
若是僅僅是一個代碼片斷,也能夠用ajaxui
$.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 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...(比ajax方便)code