第一篇博客:HTML:iframe簡要介紹
前端
我們在寫網頁的時間,有許多重復的界面,樣式和設計都一模一樣,為了避免代碼冗余,我們通常把那些界面重復的寫一個網頁,然后在需要的網頁進行參考那些重復的界面,這時就需用到iframe,
1、iframe 定義和用法 :
1.iframe一般用來包含別的頁面,例如我們可以在我們自己的網站頁面加載別人網站的內容,被稱為嵌入式框架
2.iframe 元素會創建包含另外一個檔案的行內框架(即行內框架),
2.如何使用:
<iframe src="https://www.cnblogs.com/dabaibai22/archive/2022/09/11/header.html" scrolling="no" frameborder="0" style="width: 100%; height:200px ;" ></iframe>
3.常用的一些屬性:
| width | 定義iframe的寬度 |
| height | 定義iframe的高度 |
| name | 規定iframe的名稱 |
| frameborder | 規定是否顯示邊框,0(不顯示) 、1(顯示) |
| src | 設定iframe的地址(頁面/圖片) |
| scrolling | 規定是否在iframe中顯示滾動條,屬性值(yes ,no,auto) |
| vspace | 設定或獲取物件的水平邊距 |
| hspace | 設定或獲取物件的垂直邊距 |
4.常用的一些方法
1.獲取iframe
var iframe = document.getElementById("iframe1");
2.獲取iframe的window物件
通過iframe.contentWindow
var iwindow = iframe.contentWindow
通過window.frame[‘name’] 通過這種方法可以獲取window物件
var iwindow = window.frames['name']
3.獲取iframe的document物件
通過iframe.contentDocument 獲取iframe的document物件
var idocument = iframe.contentDocument
4.在iframe中獲取父級內容
獲取上一級的window物件
window.parent
獲取最頂級容器的window物件
window.top
回傳自身window物件
window.self
5.iframe的優缺點:
優點:
a. 解決加載緩慢的第三方內容如圖示和廣告等的加載問題
b. iframe無重繪檔案上傳
c. iframe跨域通信
缺點:
d. iframe會阻塞主頁面的Onload事件
e. 無法被一些搜索引擎索引到
f. 頁面會增加服務器的http請求
g. 會產生很多頁面,不容易管理
(注:本人是個剛剛入門的小白,有很多不足的地方,請各位程式大佬指導,)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/506154.html
標籤:其他
上一篇:HTML:iframe
