问题
Possible Duplicate:
$(document).ready equivalent without jQuery
I have a script I need to write that needs to execute on DOM ready, but I can't have dependency on JQuery as part of the script.
I want to imitate $(document).ready()
, How can I implement this behavior in the shortest way?
回答1:
You can use:
document.addEventListener("DOMContentLoaded", function(){
//do things
},false);
for example. Note that this might be a problem in older browsers though, see MDN for a list of supported browsers.
回答2:
You could do 2 things. On the body tag add an onload event:
<body onload="myJSFunction()"></body>
Otherwise, you can add the functions in the bottom of the markup with:
<script type="text/javascript"> function myFunction()</script>
回答3:
window.onload = function() {
// write some javascript here
// this will be executed on page load
}
来源:https://stackoverflow.com/questions/9784346/how-to-imitate-cross-browser-document-ready-behavior-without-jquery