js函数先定义后执行
function f1(name){ alert("hello name!") } f1(wang) f2(wang) function f2(name){ alert("hello name!") }
可见放在前面和放在后的对运行结果毫无影响
例:
<body> <input type="text" name="input1" id="id1"> </body>
<script>
var ele = document.getElementById("id1");
ele.onclick =begin();
function begin() {
var currentTime = new Date().toLocaleString();
ele.value=currentTime;
setInterval(begin,1000);
}
</script>
结果显示直接弹出时间
更改代码:
for (var i=0;i<ele3.length;i++){ console.log(ele3[i]) } ele.onclick = function begin() { var currentTime = new Date().toLocaleString(); ele.value=currentTime; setInterval(begin,1000); }
点击前未生成函数:
点击后执行函数: