简体   繁体   English

按住html按钮可以启动其他A HREF吗?

[英]Can an html button launch a different A HREF when held down?

Is it possible to make one html button launch different a href? 是否可以使一个html按钮启动不同的href?

For example: if you press the button it launches w3schools.com but if you hold it down it launches google.com instead. 例如:如果您按下按钮,它将启动w3schools.com,但是如果您按住它,则将启动google.com。

I'm looking for it to work in a web app on a mobile phone, and am thinking it might be doable with the jQuery taphold Event. 我正在寻找它可以在手机上的Web应用程序中工作,并且认为它可以与jQuery taphold Event一起使用。

Or in other words: 换句话说:

<button><a href="sms:?&body=https://www.google.com">Try Me</a></button> 

if pressed and 如果按下和

<button><a href="https://www.google.com">Try Me</a></button> 

if held. 如果举行。

Any help would be much appreciated :) 任何帮助将非常感激 :)

Using tap and taphold event you can achieve your desired result. 使用tap and taphold事件,您可以实现所需的结果。

pagecreate event is fired when HTML is created in DOM 在DOM中创建HTML时会触发pagecreate事件

 $(document).on("pagecreate", function () { $.event.special.tap.emitTapOnTaphold = false; $("button").on('tap',function(){ console.log('tap!'); }).on('taphold',function(){ console.log('taphold!'); }); }); 
 <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <button>Hit me</button> 

  • Just replace the console.log line with window.open('URL') and you are done. 只需将console.log行替换为window.open('URL')
  • $.event.special.tap.emitTapOnTaphold = false; is used to disable the default click event when you tap and hold the button. 用于在按住按钮时禁用默认的单击事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM