2011-01-28 732 views

Respuesta

15

A continuación se muestra un ejemplo que muestra touchstart y touchend. Demuestra dos formas diferentes de adjuntar eventos táctiles: atributos de elementos o JavaScript addEventListener.

Como está escuchando eventos táctiles, los eventos no se dispararán en un navegador de escritorio (que admite eventos de mouse). Para probar la página, puede abrirla con un simulador de Android o 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

bien, voy a tratar .. gracias – Dayzza

+0

@mwbrroks he probado y funciona bien con un botón/enlace. Sin embargo, me preguntaba si podría funcionar en una pantalla en blanco sin ningún vínculo o botón ... lo que significa que mientras el usuario toque en cualquier parte de la pantalla, se abrirá una ventana emergente. ¿Es posible? :) gracias – Dayzza

+0

Haz un window.addEventListener (etc) – Harry

Cuestiones relacionadas