2011-01-28 35 views

回答

15

下面是一個顯示touchstarttouchend的示例。它演示了附加觸摸事件的兩種不同方式:元素屬性或JavaScript的addEventListener

由於它正在偵聽觸摸事件,因此事件不會在桌面瀏覽器(支持鼠標事件)上觸發。要測試該頁面,您可以將其打開爲Android或iOS模擬器。

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0;" /> 
    <style type="text/css"> 
     a { 
     color:black; 
     display:block; 
     margin:10px 0px; 
     } 
    </style> 

    <script type="text/javascript"> 
     function onload() { 
     document.getElementById('touchstart').addEventListener('touchstart', hello, false); 
     document.getElementById('touchend').addEventListener('touchend', bye, false); 
     } 

     function hello() { 
     alert('hello'); 
     } 

     function bye() { 
     alert('bye'); 
     } 
    </script> 

    <title>Touch Example</title> 
    </head> 
    <body onload="onload();"> 
    <h1>Touch</h1> 
    <a href="#" ontouchstart="hello();return false;">Attribute: ontouchstart</a> 
    <a href="#" ontouchend="bye();return false;">Attribute: ontouchend</a> 
    <a href="#" id="touchstart">addEventListener: touchstart</a> 
    <a href="#" id="touchend">addEventListener: touchend</a> 
    </body> 
</html> 
+0

好吧,我會盡量..謝謝 – Dayzza 2011-01-29 02:25:11