js里onload事件会执行几次? 编程,JavaScript中的onload无法多次调用,求解...
js\u91cconload\u5230\u5e95\u662f\u5e72\u561b\u7684\uff1fonload\uff0c\u5b57\u9762\u610f\u601d\u5c31\u662f\u52a0\u8f7d\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u52a0\u8f7d\u5b8c\u6210\u540e\u6267\u884c\uff0c\u8fd9\u4e2a\u4e8b\u4ef6\u4e0d\u662f\u7528\u5230\u54ea\u91cc\u90fd\u53ef\u4ee5\u7684\uff0c\u53ea\u6709\u51e0\u4e2a\u6807\u7b7e\u652f\u6301\u5b83\uff1a, , , , , , \u8fd9\u51e0\u4e2a\u6807\u7b7e\u662f\u652f\u6301\u8fd9\u4e2a\u4e8b\u4ef6\u7684\uff0c\u62ff\u6765\u8bf4\u5427\uff0c\u5982\u679c\u7528\u4e0aonload\u4e4b\u540e\u5c31\u4ee3\u8868\uff0c\u5f53\u56fe\u7247\u52a0\u8f7d\u5b8c\u6210\u540e\u6267\u884c\u67d0\u4e00\u4e2a\u65b9\u6cd5\u3002\u6bd4\u5982\u53ef\u4ee5\u7528\u6765\u68c0\u6d4b\u52a0\u8f7d\u56fe\u7247\u7528\u4e86\u591a\u4e45\uff0c\u5c31\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u6765\u8bb0\u5f55\u52a0\u8d5e\u5b8c\u6210\u7684\u65f6\u95f4\uff0c\u7136\u540e\u548c\u52a0\u8f7d\u56fe\u7247\u5f00\u59cb\u65f6\u7684\u65f6\u95f4\u5bf9\u6bd4\uff0c\u8ba1\u7b97\u51fa\u52a0\u8f7d\u8fd9\u5f20\u56fe\u7247\u6240\u7528\u7684\u65f6\u95f4\u662f\u591a\u5c11\u3002
\u4e0d\u4f46onload\uff0c\u6240\u6709\u4e8b\u4ef6\u90fd\u4e00\u6837\uff0c\u5982\u679c\u540c\u4e00\u4e2a\u4e8b\u4ef6\u4f60\u5199\u4e86\u591a\u4e2a\u89e6\u53d1\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u5b83\u53ea\u4f1a\u8fd0\u884c\u4e00\u4e2a\u7684\u3002
\u8981\u60f3\u5b9e\u73b0\u591a\u6b21\u89e6\u53d1\uff0c\u5fc5\u987b\u4f7f\u7528addEventListener--\u201c\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u542c\u201d\u7684\u65b9\u6cd5\uff1a
window.addEventListener('load',\u51fd\u65701,false);
window.addEventListener('load',\u51fd\u65702,false);
\u8981\u6ce8\u610f\uff0c\u8fd9\u4e24\u4e2a\uff08\u6216\u8005\u66f4\u591a\uff09\u8bed\u53e5\u53ef\u4ee5\u653e\u5728\u9875\u9762\u7684\u4e0d\u540c\u5730\u65b9\uff08\u751a\u81f3\u53ef\u4ee5\u662f\u4e0d\u540c\u7684js\u6587\u4ef6\u5185\uff09\uff0c\u5f53\u7f51\u9875\u52a0\u8f7d\uff08onload\uff09\u65f6\uff0c\u8fd9\u4e9b\u51fd\u6570\u5c31\u4f1a\u9010\u4e00\u8fd0\u884c\u4e86\u3002
\u4f46\u662f\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u5728IE\u4e2d\u65e0\u6548\uff0cIE\u4e2d\u8981\u7528attachEvent\uff0c\u800c\u4e14\u7528\u6cd5\u4e5f\u4e0d\u540c\uff1a
window.attachEvent('onload',\u51fd\u65701);
window.attachEvent('onload',\u51fd\u65702);
执行一次。
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function () {
oldonload();
func();
}
}
}
function addload() {
alert("addload");
}
// 执行到这里的时候直接弹出onload,与onload事件无关
window.onload = alert("onload");
// 监听onload事件,所以onload事件触发之后才会弹出addload
addLoadEvent(addload);
// 执行到这里的时候直接弹出last,与onload事件无关
alert("last");
绛旓細window.onload鐨鎰忔濇槸锛浜嬩欢浼鍦ㄩ〉闈㈠姞杞藉畬鎴愬悗瑙﹀彂銆備緥濡傦細<!doctype html> window.onload绀轰緥 window.onload = function(){ alert('椤甸潰鍔犺浇瀹屾垚');} alert('椤甸潰灏氭湭鍔犺浇瀹屾垚,椤甸潰鍐呭涓嶆樉绀'); 椤甸潰鍐呭 鐢ㄦ硶锛氫竴鑸彲浠ョ敤杩欎釜浜嬩欢鍋氫竴浜涢〉闈㈡暟鎹垵濮嬪寲涔嬬被鐨勫伐浣溿
绛旓細浣跨敤img.onload鐨勪竴涓富瑕佷紭鐐规槸锛屽畠鍙互纭繚鍦鎵ц鍚庣画鎿嶄綔涔嬪墠鍥惧儚宸茬粡瀹屽叏鍔犺浇銆傝繖鏍峰彲浠ラ伩鍏嶅洜鍥惧儚灏氭湭鍔犺浇瀹屾垚鑰屽鑷寸殑甯冨眬闂鎴栭敊璇備娇鐢╥mg.onload浜嬩欢鐨鏃舵満閫氬父鏄湪浣犺缃簡鍥惧儚鐨剆rc灞炴т箣鍚庛傝繖鏍凤紝褰搊nload浜嬩欢瑙﹀彂鏃讹紝浣犲彲浠ョ‘淇″浘鍍忓凡缁忓姞杞藉畬鎴愩傞渶瑕佹敞鎰忕殑鏄紝onload浜嬩欢鍙細鍦ㄥ浘鍍忔垚鍔熷姞杞芥椂...
绛旓細涓銆佷娇鐢ㄦ椂鏈轰笉鍚 1銆onload锛氬綋鐢ㄦ埛杩涘叆椤甸潰鏃灏变細瑙﹀彂銆2銆乨ocument.ready锛氭槸褰揇OM杞藉叆灏辩华鍙互鏌ヨ鍙婃搷绾垫椂缁戝畾涓涓鎵ц鐨鍑芥暟銆備簩銆佷綔鐢ㄤ笉鍚 1銆乷nload锛氫簨浠跺父鐢ㄦ潵妫娴嬭闂呯殑娴忚鍣ㄧ被鍨嬪拰鐗堟湰锛岀劧鍚庢牴鎹繖浜涗俊鎭浇鍏ョ壒瀹氱増鏈殑缃戦〉銆2銆乨ocument.ready锛氭槸瀵瑰悜window.load浜嬩欢娉ㄥ唽浜嬩欢鐨勬浛浠f柟娉曘
绛旓細鏄痺indow.onload浜嬩欢銆備絾娴忚鍣ㄩ殢鏃堕兘鍙互鍒╃敤ajax鍚戞湇鍔″櫒鍙戦佽姹傘俹nload 鏌愪釜椤甸潰鎴栧浘鍍忚瀹屾垚鍔犺浇 灞炴у綋浠ヤ笅鎯呭喌鍙戠敓鏃讹紝鍑虹幇姝や簨浠禙FNIE onabort 鍥惧儚鍔犺浇琚腑鏂 1 3 4 onblur 鍏冪礌澶卞幓鐒︾偣 1 2 3 onchange 鐢ㄦ埛鏀瑰彉鍩鐨鍐呭 1 2 3 onclick 榧犳爣...
绛旓細1鍦╰est.html椤甸潰锛屼娇鐢╢unction鍒涘缓涓涓猰yfun鍑芥暟锛屼唬鐮佸涓嬶細2銆佸湪myfun鍑芥暟鍐咃紝浣跨敤alert()鏂规硶寮瑰嚭鈥渕yfun鍑芥暟鎵ц浜嗏濈殑鏂囧瓧鎻愮ず锛屼唬鐮佸涓嬨3銆佸湪test.html涓殑body鏍囩鍚庨潰锛屼娇鐢╫nload鍦ㄩ〉闈㈡墦寮鏃跺氨鍔犺浇鎵цmyfun()鍑芥暟銆4銆onload浜嬩欢涓嶄粎鍙互鏀惧湪body鏍囩鍐咃紝涔熷彲浠ョ洿鎺ュ湪js鏍囩鍐呬娇鐢紝浠g爜濡備笅銆...
绛旓細onload鍙兘鏈変竴涓紝瑙e喅鏂规浣犲彲浠ュ啓鍒颁竴涓onload閲 鎴栬 ie涓嬬敤window.attachEvent("onload", 浣鐨刼nload鏂规硶)闈瀒e涓嬬敤window.addEventListener("load", 浣犵殑onload鏂规硶)杩欐牱娣诲姞onload浜嬩欢
绛旓細姣斿onload浜嬩欢閲岄潰锛鎵ц浜嗕竴娈典唬鐮侊紝鎵ц瀹屾垚涔嬪悗锛屾兂璁╁畠杩5绉掓墽琛屽彟涓娈典唬鐮: function abc(){ //鎵ц浣鐨浠g爜 setTimeout("bbb()",5000);//璁剧疆5绉掗挓鍚庢墽琛宐bb鏂规硶 } function bbb(){ alert("123");}
绛旓細鍦JS涓锛屽鏋滃笇鏈涘湪椤甸潰鍔犺浇瀹屾垚鍚鎵ц鏌愪簺鎿嶄綔鐨勮瘽锛屼竴鑸殑鍐欐硶濡備笅锛歸indow.onload = function(){//ToDo};浣嗘槸锛宱nload鍑芥暟鐨勬墽琛屽苟涓嶆槸鎴戜滑閫氬父鎵鐞嗚В鐨勬祻瑙堝櫒鏋勫缓瀹宧tml椤甸潰瀵瑰簲鐨凞OM鏍戝悗绔嬪埢鎵ц锛岃屾槸瑕佺瓑鍒伴〉闈腑鎵寮曠敤鐨勫浘鐗囩瓑澶栭儴璧勬簮 瀹屽叏涓嬭浇瀹屾垚鍚庡苟涓斿湪娴忚鍣ㄤ腑鏄剧ず鎵嶆墽琛岋紝涔熷氨鏄锛屽鏋...
绛旓細鍙互銆 閮藉彲浠 浣犺瘯涓嬶紒锛
绛旓細2銆佸湪index.html涓殑鏍囩涓緭鍏ワ細btn锛屾爣绛句腑杈撳叆js浠g爜锛歠unction send() {document.body.innerText = 'use send';}銆3銆佹祻瑙堝櫒杩愯index.html椤甸潰锛岀偣鍑籦tn鎸夐挳銆4銆佺瓑寰1绉掑悗锛岀‘瀹鎵ц浜唖end()鏂规硶鎵撳嵃浜嗏渦se send鈥濇枃鏈