Dom EVENT对象

Event 对象代表事件的状态,比如事件在其中发生的元素、键盘按键的状态、鼠标的位置、鼠标按钮的状态。

事件通常与函数结合使用,函数不会在事件发生前被执行!

一:测试按键

function whichButton(event){
  var btnNum = event.button;
if (btnNum==2){
  alert("您点击了鼠标右键!") } else if(btnNum==0) { alert("您点击了鼠标左键!") } else if(btnNum==1) { alert("您点击了鼠标中键!"); } else { alert("您点击了" + btnNum+ "号键,我不能确定它的名称。"); } }

二:获取鼠标坐标

function show_coords(event)
{
  x=event.clientX;
  y=event.clientY;
  alert("X 坐标: " + x + ", Y 坐标: " + y) }

  

三:获取 键盘 按键的 unicode 

function whichButton(event)
{
  alert(event.keyCode);
}

  

四:相对于屏幕的 鼠标 坐标

function coordinates(event)
{
  x=event.screenX;
  y=event.screenY;
  alert("X=" + x + " Y=" + y); }

  

五:shift 按键是否被  按住

function isKeyPressed(event)
{
  if (event.shiftKey==1)
    {
      alert("The shift key was pressed!")
    }
  else
    {
      alert("The shift key was NOT pressed!")
    }
  }

  

六:测试 点击了 何种元素(p,div,或者 image,a and so on.)

function whichElement(e)
{
  var targ;
  if (!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3); // defeat Safari bug
targ = targ.parentNode;
  var tname;
  tname=targ.tagName;
  alert("You clicked on a " + tname + " element."); }

  

猜你喜欢

转载自www.cnblogs.com/moutudou/p/9003845.html
今日推荐