http://www.quirksmode.org/js/events_tradmod.html
这里有点解释:
看这段
No parentheses!Please note that in the registration of an event handler you do not use parentheses (). The onclick method expects to be assigned an entire function. If you’d doelement.onclick = doSomething();
the function would be executed and its result would be registered to onclick. This is not what we want, we want the function to be executed when the event takes place. In addition the function has usually been written to expect an event and if we’d execute it without any such context it would get terribly confused and produce JavaScript errors.Instead we copy the function doSomething() to the event handler in its entirety. We do not execute it yet, that should only happen when the event actually takes place.加了括号就不光是注册event了,会执行他,然后把结果注册给event。
至于怎么把结果给注册到event上,这个结果是什么,希望大神们补充吧,我还没搞懂
解决方案 »
- JSP修改程序的端口!在线等
- submit
- 各位师傅求救!我遇到一个问题,就是页面中用iframe嵌套的内容在浏览器中可以拖动?怎么回事?怎样禁止拖动?
- 我用window.open("xxx.asp","", "...,fullscreen=yes,...")全屏打开个页面.页面里的body用onload="top.resizeTo(300,200);"怎么不起作用
- ie浏览器 js 怎么判断是否安装Google Chrome Frame插件
- 为空问题
- 在javascript中,怎么处理"\"??
- 如何通过在客户端的html页面,来执行服务端机器里的可执行文件
- 求最简单的字符加密算法.
- 【JS能轻松实现?小弟不才坐等高手】<P> 标记定义不同文字。
- js日历出错,帮忙查找下原因。
- 一个jq点击隐藏显示的问题!
补充下:
.onclick = dianJi
dianji 这个方法名指向的是整个函数体,就是上面你定义的执行alter的那块但.onclick = dianJi();
实际上相当于 a = max(3,5);
首先会执行 max(3,5) 函数本身,然后执行 赋值 = , 把 返回值 给了 a这里就是先执行dianJi(),然后dianJi()里的返回值注册给了onclick();
楼主疑惑每次刷新页面会alert(),很简单,因为script标签里的代码被俺顺序解释执行了,你直接在script标签里写个alert(),外面不包裹function{},一个道理。