$ ready
$(document).ready(function() { //all your code goes here... });
ready: (function() { ... //Mozilla, Opera, and Webkit if (document.addEventListener) { document.addEventListener("DOMContentLoaded", idempotent_fn, false); ... } //IE event model else if (document.attachEvent) { // ensure firing before onload; maybe late but safe also for iframes document.attachEvent("onreadystatechange", idempotent_fn); // A fallback to window.onload, that will always work window.attachEvent("onload", idempotent_fn); ... } })
网上看到的一段代码,查了一下zepto源码文档
ready: function(callback){ if (readyRE.test(document.readyState)) callback($) else document.addEventListener('DOMContentLoaded', function(){ callback($) }, false) return this }
小demo:
var ee = { ready: function(callback){ callback(); } }; ee.ready(function(){ alert('bad'); });