事件冒泡或事件捕获
addEventListener(event, function, useCapture);
默认值为 false, 即冒泡传递,当值为 true 时, 事件使用捕获传递。
取消事件冒泡
~~~
function stopBubble(event){
if(event.stopPropagation){
event.stopPropagation();
}else{
event.cancelBubble = true;
}
}
~~~
鼠标事件
click:点击 = (mousedown:按下+mouseup:弹起)
mousemove:移动
mouseover:鼠标盖住当前区域=mouseenter
mouseout:鼠标移出当前区域=mouseleave
contextmenu:监听右键,常用语取消菜单
用e.button获取鼠标的按键,0:左键,1:滚轮,2:右键
鼠标点击拖拽移动
~~~
div.onmousedown = function (e) {
disX = e.pageX - parseInt(div.style.left);
disY = e.pageY - parseInt(div.style.top);
document.onmousemove = function (e) {
var event = e || window.event;
div.style.left = e.pageX - disX + "px";
div.style.top = e.pageY - disY + "px";
}
document.onmouseup = function (e) {
document.onmousemove = null;
}
}
~~~
键盘事件
keydown:键盘按下,可以响应**任意**键盘按键,但是无法区分大小写
keypress:只可以响应**字符类**键盘按键,返回ASCII码。可以转换成相应的字符
keyup:键盘弹起