HTML touchcancel事件实例讲解

时间:2022-04-06
本文章向大家介绍HTML touchcancel事件实例讲解,主要分析其语法、参数、返回值和注意事项,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

当触摸事件被中断时,touchcancel事件用于执行脚本。如果设备以不同的动作中断触摸事件,则包括touchcancel事件以清理代码是一种很好的做法。

用法:

object.ontouchcancel = myScript;

下面的程序演示了touchcancel事件:示例1:触摸中断时执行JavaScript。


<!DOCTYPE html> 
<html> 
  
<head> 
    <title>touchcancel Event in HTML</title> 
    <style> 
        h1 { 
            color:green; 
        } 
          
        h2 { 
            font-family:Impact; 
        } 
          
        body { 
            text-align:center; 
        } 
    </style> 
</head> 
  
<body> 
  
    <h1>GeeksforGeeks</h1> 
    <h2>touchcancel Event</h2> 
    <br> 
  
    <p ontouchcancel="cancel()"> 
      Touch somewhere in the paragraph while 
      you do something that will interrupt the event.</p> 
    <br> 
  
    <p id="test"></p> 
  
    <script> 
        function cancel() { 
            
            document.getElementById( 
              "test").innerHTML =  
              "Touch Cancelled due to interruption"; 
        } 
    </script> 
  
</body> 
  
</html>

输出:
触摸屏幕之前:

触摸屏幕后:

支持的浏览器:

  • IE浏览器
  • 谷歌浏览器
  • Firefox