HTML事件属性onload

2018-01-05 13:58 更新

HTML事件属性onload


当加载元素时触发 onload 属性事件。

onload 最常用于< body> 元件以在网页已完全加载全部时执行脚本内容,包括图像,脚本文件,CSS文件等。

我们也可以使用 onload attribute event和iframe。

HTML5中的新功能

没有。

句法

<element onload="script or Javascript function name">

支持的标签

<body>, 
<frame>, 
<frameset>, 
<iframe>, 
<img>, 
<input type="image">, 
<link>, 
<script> 
<style>


浏览器兼容性

onload Yes Yes Yes Yes Yes

例子

以下代码处理body元素上的onload事件。

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
    alert("Page is loaded");
    
    if (navigator.cookieEnabled == true) {
        alert("Cookies are enabled.");
    } else {
        alert("Cookies are not enabled.");
    }    
}
</script>
</head>

<body onload="myFunction()">
   <h1>Hello World!</h1>
   
</body>

</html>

Click to view the demo



实施例2

以下代码处理图像元素的onload事件。

<!DOCTYPE html>
<html>
<body>

<img src="/attachments/jimg/border.png" onload="loadImage()" width="100" height="100">

<script>
function loadImage() {
    alert("Image is loaded");
}
</script>

</body>
</html>

Click to view the demo

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号