HTML onload 屬性
定義和用法
onload 屬性在物件載入完成後觸發。
onload 最常用於 <body> 元素中,以在網頁完全載入所有內容(包括影像、指令碼檔案、CSS 檔案等)後執行指令碼。但是,它也可以用於其他元素(參見下面的“支援的 HTML 標籤”)。
對於輸入元素,onload 屬性僅在 <input type="image"> 時受支援
onload 屬性可用於檢查訪問者的瀏覽器型別和瀏覽器版本,並根據資訊載入相應版本的網頁。
適用範圍
onload 屬性是 事件屬性 的一部分,可用於以下元素
| 元素 |
|---|
| <body> |
| <iframe> |
| <img> |
| <input> |
| <link> |
| <script> |
| <style> |
示例
圖片示例
在 <img> 元素上使用 onload。影像載入後立即提示“影像已載入”
<img src="w3html.gif" onload="loadImage()" width="100" height="132">
<script>
function loadImage() {
alert("圖片已載入");
}
</script>
自己動手試一試 »
Input 示例
在 <input type="image"> 元素上使用 onload。圖片載入後立即彈出“圖片已載入”
<input type="image" onload="loadImage()" src="img_submit.gif" alt="提交">
<script>
function loadImage() {
alert("圖片已載入");
}
</script>
自己動手試一試 »
瀏覽器支援
onload 屬性對每個元素的瀏覽器支援如下
| 元素 | |||||
|---|---|---|---|---|---|
| body | 是 | 是 | 是 | 是 | 是 |
| iframe | 是 | 是 | 是 | 是 | 是 |
| img | 是 | 是 | 是 | 是 | 是 |
| input type="image" | 是 | 是 | 是 | 是 | 是 |
| 連結 | 是 | 是 | 是 | 是 | 是 |
| script | 是 | 是 | 是 | 是 | 是 |
| 樣式 | 是 | 是 | 是 | 是 | 是 |