亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用JS附加body onload事件

使用JS附加body onload事件

紅顏莎娜 2019-11-30 10:47:13
如何通過跨瀏覽器方式將主體onload事件與JS關聯?這樣簡單嗎?  document.body.onload = function(){      alert("LOADED!");  }
查看完整描述

3 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

這利用了DOMContentLoaded的優點-在onload之前會觸發-但允許您堅持所有不引人注意的內容...


window.onload-Dean Edwards-該博客文章討論了更多內容-這是從同一博客的評論中復制的完整代碼。


// Dean Edwards/Matthias Miller/John Resig


function init() {

  // quit if this function has already been called

  if (arguments.callee.done) return;


  // flag this function so we don't do the same thing twice

  arguments.callee.done = true;


  // kill the timer

  if (_timer) clearInterval(_timer);


  // do stuff

};


/* for Mozilla/Opera9 */

if (document.addEventListener) {

  document.addEventListener("DOMContentLoaded", init, false);

}


/* for Internet Explorer */

/*@cc_on @*/

/*@if (@_win32)

  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");

  var script = document.getElementById("__ie_onload");

  script.onreadystatechange = function() {

    if (this.readyState == "complete") {

      init(); // call the onload handler

    }

  };

/*@end @*/


/* for Safari */

if (/WebKit/i.test(navigator.userAgent)) { // sniff

  var _timer = setInterval(function() {

    if (/loaded|complete/.test(document.readyState)) {

      init(); // call the onload handler

    }

  }, 10);

}


/* for other browsers */

window.onload = init;


查看完整回答
反對 回復 2019-11-30
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

為什么不使用window自己的onload事件?


window.onload = function () {

      alert("LOADED!");

}

如果我沒記錯的話,那么所有瀏覽器都兼容。


查看完整回答
反對 回復 2019-11-30
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

jcalfee314的想法對我有用-我有一個window.onload = onLoad含義,即其中的函數<body onload="...">沒有被調用(我無法控制)。


這修復了它:


oldOnLoad = window.onload

window.onload = onLoad;


function onLoad()

{

oldOnLoad();

...

}

編輯:Firefox不喜歡oldOnLoad = document.body.onload;,所以替換為oldOnLoad = window.onload。


查看完整回答
反對 回復 2019-11-30
  • 3 回答
  • 0 關注
  • 620 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號