如果在已加载的元素(例如iframe)上调用onload()会发生什么?
假设myIframe已经加载,我执行以下命令:
myIframe.onload = function () { console.log('iframe loaded') };
它将执行吗?
答案 0 :(得分:0)
myIframe.onload() = function () { console.log('iframe loaded') };
会导致错误未捕获的ReferenceError:分配中的左侧无效
iframe.addEventListener("load", () => console.log("iframe loaded"));
var event = new Event("load");
iframe.dispatchEvent(event);
var iframe = document.getElementById("frame");
iframe.addEventListener("load", () => console.log("iframe loaded"));
var event = new Event("load");
iframe.dispatchEvent(event);
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>
Functions
</title>
</head>
<body>
<iframe id="frame" src="http://support.worldpay.com/support/kb/gg/corporate-gateway-guide/content/resources/images/hpp_iframe_-_paymt_mthd_select_page_04.jpg"></iframe>
</body>
</html>