简体   繁体   English

防止在输入集中时单击锚链接?

[英]Prevent clicking an anchor link while an input is focused?

I'm attempting to prevent a user from clicking a link and it going to another page while any input is focused. 我试图防止用户在任何输入都处于焦点时单击链接并将其转到另一个页面。 Sometimes the only available space around the input and the keyboard is a link and some users click the link accidentally. 有时,输入和键盘周围唯一可用的空间是链接,某些用户不小心单击了该链接。 I'm trying to make it so when they click the link it blurs the input (closes the keyboard) and prevents the page from following the link. 我试图做到这一点,以便当他们单击链接时,它会使输入模糊(关闭键盘),并阻止页面跟随链接。 Then let them click the link again if they want to go to another page after the input is no longer in focus. 然后,如果他们想在输入不再焦点后转到另一个页面,则让他们再次单击该链接。

html html

<input type="text">
<a href="example.com">Example</a>

I've tried the following... 我尝试了以下方法...

jQuery jQuery的

$('a').on('click', function (event) {
   if ($('input').is(":focus")) {
      console.log('focused');
      event.preventDefault();
   }
});

(nothing gets logged) (什么都没有记录)

Also tried this... 还尝试了这个...

if ($('input').is(":focus")) {
    $('a').on('click', function (event) {
      event.preventDefault();
      $('input').each(function(){
        $(this).trigger('blur');
      });
    });
  }

Neither one prevent the page from going to whatever link was clicked... 没有人阻止该页面转到任何单击的链接...

I don't think you can do this. 我认为您无法做到这一点。 You can disable the click event on the links while input is focused, and enable it back again when blur occurs on the input elements. 您可以在input集中时禁用链接上的click事件,并在input元素上发生blur时再次启用它。 However, while if user clicks on a link while focused on the input element blur event will occur first (which would enable clicking) then click even occurs and links acts as normal. 但是,如果用户单击链接时集中在input元素上,则将首先发生blur事件(这将使单击成为可能),然后甚至发生click ,链接也将正常运行。

You could try disabling the links while input elements have focus, then you can enable them on the first click and restore normal operation. 您可以尝试在input元素具有焦点时禁用链接,然后可以在第一次单击时将其启用并恢复正常操作。

 $("input").on("focus", function() { $("a").on("click", function (event) { event.preventDefault(); $("a").off(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" /> <a href="http://example.com/">example</a> 

I think I found a solution. 我想我找到了解决方案。

HTML 的HTML

Example.com Example.com

jQuery jQuery的

$('input').on('focus', function () {
      $('a').each(function(){
        $(this).addClass('cant-click');
      });
});

$(document).on('touchend', function (e) {
    if (!$(e.target).closest('input').length) {
      $('a').each(function(){
        $(this).removeClass('cant-click');
      });
    }
});

CSS 的CSS

a.cant-click { pointer-events: none; }

When the input takes focus, every link gets this class. 当输入获得焦点时,每个链接都将获得此类。 When anything on the page is clicked that is not an input, it removes this class from every link. 单击页面上不是输入的任何内容时,它将从每个链接中删除该类。

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

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