頁面的引入是不少用戶想實現的,一般情況下我們會使用iframe的方式來引入
<body> <div id="div"> <iframe align="center" width="100%" height="200" src="index.html" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe> </div> </body>
但是有的時候我們發現有些JS或者聯盟是禁止使用IFRAME引入的,如果用iframe會出現提示:iframe not allow!
因此這個時候我們需要通過其他的方式來實現index.html的頁面引入。
1、通過jq的load方法引入
<body> <div id="div"></div> <script> $("#div").load("index.html"); </script></body>
2、原生js的object標簽引入
<body> <div id="div"></div> <script> document.getElementById("div").innerHTML = '<object type="text/html" data="index.html" width="100%" height="100%"></object>'; </script></body>
3、import引入(head中引入文件,script中加載內容)
href鏈接引入的html文件,id可以看做頁面引導,在script中用到
<head> <meta charset="utf-8" /> <title>主頁面</title> <!--import引入--> <link rel="import" href="top.html" id="page1"/> <link rel="import" href="fotter.html" id="page2"/></head>
<!--注意順序--><!--import在頭部引入,里面是啥就是啥--><script type="text/javascript"> document.write(page1.import.body.innerHTML);</script>你好呀!<!--本頁面寫入內容--><script type="text/javascript"> document.write(page2.import.body.innerHTML);</script以上就是js實現一個html頁面引入另一個html頁面的全部內容,希望對大家的學習和解決疑問有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答