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上,这个结果是什么,希望大神们补充吧,我还没搞懂

解决方案 »

  1.   


    补充下:
    .onclick = dianJi
    dianji 这个方法名指向的是整个函数体,就是上面你定义的执行alter的那块但.onclick = dianJi();
    实际上相当于  a = max(3,5);
    首先会执行 max(3,5) 函数本身,然后执行 赋值 = ,  把 返回值 给了 a这里就是先执行dianJi(),然后dianJi()里的返回值注册给了onclick();
      

  2.   

    基本语法,甚至与js都没关系,程序编写的最基本知识了,dianJi表示的是函数,dianJi()表示函数返回值,js中没return返回值则返回值为undefined
      

  3.   

    膜拜啊,您费心了。另外说, .onclick 是不是只能接受函数名或匿名函数,如果给个 .onclick = 'dianJi()' 它就会把字符串赋给事件,而不是字符串里的函数名是吧?谢谢你。
      

  4.   

    简单的说,注册事件其实也是赋值,只不过是把函数的地址赋上去,但要是加括号,那就执行这个方法了,方法的执行是有返回值的,所以把返回值给赋过去了。
    楼主疑惑每次刷新页面会alert(),很简单,因为script标签里的代码被俺顺序解释执行了,你直接在script标签里写个alert(),外面不包裹function{},一个道理。