JS-DOM:Event 對象

來源:互聯網
上載者:User

標籤:style   color   java   io   strong   ar   cti   div   

事件(event)由事件及事件方法組成: 

(事件對象的相容寫法:window.event||event)

1、滑鼠事件:

mousedown、mousemove、mouseup、ondbclick、contextmenu...

2、表單事件:

focus、blur、submit、change...

3、鍵盤事件

keydown、keyup、keypress...

4、阻止冒泡

oEvent.cancelBubble=true;

5、阻止預設事件

return false;

6、鍵盤方法

keyCode

 

1、onblur :失去焦點

<script>

window.onload=function(){

  var oTxt=document.getElementById("txt1");

  oTxt.onblur=function(){

    alert("失去焦點");

  }

}

</script>

 

2、oncontextmenu:用oncontextmenu事件單禁用右鍵菜單

<script>

document.oncontextmenu=function(){

    alert(‘a‘);

  }

</script>

 

3 、ondblclick: 滑鼠雙擊事件

<srcipt>

window.ondblclick=function(){

  alert(‘a‘);

}

</script>

 

4、onfocus: onFocus事件就是當游標落在文字框中時發生的事件

<script>

window.onload=function(){

  var oTxt=document.getElementById("txt1");

  oTxt.onfocus=function(){

    alert("聚焦了");

  }

}

</script>

<body>
<input type="text" id="txt1" value="請輸入文本">
</body>

 

5、onmousedown : 滑鼠按鍵被按下  ... onmouse up : 滑鼠按鍵被鬆開

<style type="text/css">
#div1{
    height: 100px;
    width: 100px;
    background-color: #ccc;
}
</style>


<script type="text/javascript">
window.onload=function (){

    document.onmousedown=function(){

    alert(‘a‘);

  }
}
</script>

 

<script type="text/javascript">
window.onload=function (){

    document.onmouseup=function(){

    alert(‘a‘);

  }
}
</script>



<body>
<div id="div1"></div>
</body>

 

6、onmousemove :滑鼠被移動

<style type="text/css">
#div1{
    height: 100px;
    width: 100px;
    background-color: #ccc;
}
</style>


<script type="text/javascript">
window.onload=function(){

  var oDiv=document.getElementById("div1");

  document.onmousemove=function(ev){

    var oEvent=ev||window.event;

    document.title=oEvent.clientX;

    oDiv.style.left=oEvent.clientX+"px";

    oDiv.style.top=oEvent.clientY+"px";

  }

}
</script>

<body>
<div id="div1"></div>
</body>

 

未完待續。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.