标签:
比如进行页面显示初始化的js必须放在head里面,因为初始化都要求提前进行(如给页面body设置css等);而如果是通过事件调用执行的function那么对位置没什么要求的。
<script>/* document.onclick = function () {alert("You have clicked in the document!");};var img = document.getElementById("mainImage");img.onclick = function () {alert("You have clicked on the picture!");};*//* 若将document.onclick放在img onclick事件之前,那么只会提示document的onclick *//* 若将img onclick放在document.onclick事件之前,那么无任何提示*/</script>
<body><img src="images/image.jpg" id="mainImage">document.onclick = function () {alert("You have clicked in the document!");};var img = document.getElementById("mainImage");img.onclick = function () {alert("You have clicked on the picture!");};//此时document.onclick与img.onclick的顺序不影响执行结果</body>
<script>function Demo() {document.onclick = function () {alert("You have clicked in the document!");};var img = document.getElementById("mainImage");img.onclick = function () {alert("You have clicked on the picture!");};}window.onload = function () {Demo();}</script>
标签:
原文地址:http://www.cnblogs.com/Jener/p/5982183.html