close

在網頁中 , 若我們想在使用者在按下Button時而觸發event , 基本上的做法有兩種:

1. 在input 的tag中註明click後所觸發的function.

2. 用Javascript取得html element , 並藉由onmousedown來決定要trigger的function.

 

1. 

<input type = "button" id="b1" onClick="alert('you clicked button 1!!')" value="button1">

 

2. 

<script type="text/javascript">

function getButton()

{

switch(event.button)

{

case 0: // 0 -> left key on mouse

alert('you clicked button 2!!');

break;

case 1: // 1 -> middle key on mouse

alert('you clicked button 2!!');

break;

case 2: // 2 -> right key on mouse

alert('you clicked button 2!!');

break;

}

}


</script>

<input type = "button" id="b2" value="button2">

<script>

var button2 = document.getElementById("b2");

button2.onmousedown = getButton;

</script>

 

Attention : 

在W3C中 , 定義 0 , 1 , 2 分別為left , middle , 以及right key

但是這在不同的browser中可能會有差異 , 所以最安全的方式還是使用mousedown以及mouseup來判斷.

 

ref : http://www.w3schools.com/jsref/event_onmousedown.asp

ref : http://www.quirksmode.org/js/events_properties.html

arrow
arrow
    文章標籤
    javascript mouse event
    全站熱搜
    創作者介紹
    創作者 JerryCheng 的頭像
    JerryCheng

    KwCheng's blog

    JerryCheng 發表在 痞客邦 留言(0) 人氣()