看一看html的帮助文件里是怎么说的:onclick被触发的几种:情况 1.Click the object. 2.Invoke the click method. 3.Press the ENTER key in a form. 4.Press the access key for a control. 5.Select an item in a combo box or list box by clicking the left mouse button or by pressing the arrow keys and then pressing the ENTER key. 对于第一种情况,也就是楼主所提到的,作了特别的说明: If the user clicks the left mouse button, the onclick event for an object occurs only if the mouse pointer is over the object and an onmousedown and an onmouseup event occur in that order. For example, if the user clicks the mouse on the object but moves the mouse pointer away from the object before releasing, no onclick event occurs.
这是整个文件,试试
如果你这两个事件都用鼠标点击,肯定是执行onmousedown在先,而不会执行onclick
如果你用键盘测试的话,你就能做出效果了,我想做了之后,能明显找出两者的区别
可是使用<input type="button" value="hello" onmousedown=var iii='1111'; onclick=alert(iii);>就能打出iii.
使用<input type="button" value="world" onmousedown=var iii='1111';alert("22222"); onclick=alert(iii);>alert("22222")打不出来.
一次配对的mousedown和mouseup 都发生在同一个对象上,才叫触发click事件。
以上的代码,mousedown后,焦点立即跑到alert对话框上去了,跟这个mousedown配对的mouseup不可能还发生在这个button上。所以这个button的click事件无法被触发。
1.Click the object.
2.Invoke the click method.
3.Press the ENTER key in a form.
4.Press the access key for a control.
5.Select an item in a combo box or list box by clicking the left mouse button or by pressing the arrow keys and then pressing the ENTER key. 对于第一种情况,也就是楼主所提到的,作了特别的说明:
If the user clicks the left mouse button, the onclick event for an object occurs only if the mouse pointer is over the object and an onmousedown and an onmouseup event occur in that order. For example, if the user clicks the mouse on the object but moves the mouse pointer away from the object before releasing, no onclick event occurs.
现在还有一个问题:
<input type="button" value="world" onmousedown="javascript:var iii='1111';" onclick=alert(iii);>
问何提示iii未定义,这个郁闷.
<input type="button" value="world" onmousedown="window.iii='222'; " onclick=alert(window.iii);>
<input type="button" value="world" onmousedown="this.iii='333'; " onclick=alert(this.iii);>