javascript 点击锚标签链接回车按

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13797894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:49:07  来源:igfitidea点击:

Click anchor tag link on enter press

javascriptjquerygoogle-chrome

提问by Sanjib Karmakar

I have an anchor tag like

我有一个锚标签,比如

<a class="btn btn-danger" id="clicking" data-bind="click: $root.enterLocation" href="#">Continue</a>

It's inside a pop-up. I need to click on this link on pressing enter key. I have tried the following code but it did not work for me.

它在一个弹出窗口中。我需要按回车键点击这个链接。我尝试了以下代码,但它对我不起作用。

$(document).ready(function(){ 
  $(document).keyup(function(event){
    if (event.keyCode == 13){
      $("#clicking").trigger('click');     
    }
  })
});

Not sure why the functionality is not working. I have used click function also with same result.Its working correctly on mouse click. I need to make it work automatically on enter press.

不知道为什么该功能不起作用。我也使用了点击功能,结果相同。鼠标点击时它可以正常工作。我需要让它在回车时自动工作。

Following code is working fine in Firefox.

以下代码在 Firefox 中运行良好。

$(document).ready(function () {
  $(document).on("keyup", function (event) {
    if (event.which == 13) {
      document.getElementById("clicking").click();   
    }
  });
}); 

How to make this work on Chrome?

如何在 Chrome 上进行这项工作?

回答by Ian

I think the problem is that you're using event.keyCode, which isn't always used in all browsers. Some browsers use event.charCodeor even a different event.which, which may be supported by what you're using. Anyways, the normal way to get the keycode from the event with jQuery is to use event.which.

我认为问题在于您使用的event.keyCode是 ,并不总是在所有浏览器中使用。某些浏览器使用event.charCode或什至不同的event.which,这可能受您使用的支持。无论如何,使用 jQuery 从事件中获取键码的正常方法是使用event.which.

jQuery normalizes the eventobject passed to the event handler and fixes "problems" like this so that you don't have to worry. At the same time, it seems that it copies over some of the original event's properties ("Most properties from the original event are copied over and normalized to the new event object." - from the jQuery API docs). That's probably why it's "working" for the other people commenting/answering. The eventparameter passed to the handler has been generated/normalized by jQuery and will have everything you need, using the right properties. The correct way though, is to use event.whichto get a normalized keycode for the event. http://api.jquery.com/event.which/

jQuery 对event传递给事件处理程序的对象进行规范化并修复这样的“问题”,这样您就不必担心了。同时,它似乎复制了一些原始事件的属性(“来自原始事件的大多数属性都被复制并规范化为新的事件对象。” - 来自 jQuery API 文档)。这可能就是为什么它对其他评论/回答的人“有效”的原因。event传递给处理程序的参数已由 jQuery 生成/规范化,并且将使用正确的属性提供您需要的一切。正确的方法是使用event.which获取事件的规范化键码。http://api.jquery.com/event.which/

$(document).ready(function () {
    $(document).on("keyup", function (event) {
        if (event.which == 13) {
            $("#clicking").trigger('click');
        }
    });
});

回答by Ram Lakhan Yadav

I hope following code can help you .try it Code:

我希望下面的代码可以帮助你尝试一下代码:

<a class="btn btn-danger" id="clicking" onclick="window.location='index.php'" href="#">Continue</a>

$(function(){
  $('body').keyup(function(e){
    if (e.keyCode == 13){  
      $("#clicking").click();
    }
  })
});

回答by Dillen Meijboom

I've created this JSFiddle: http://jsfiddle.net/egzsf/It works perfectly I only added a fallback for Internet Explorer.

我已经创建了这个 JSFiddle:http: //jsfiddle.net/egzsf/它工作得很好,我只为 Internet Explorer 添加了一个后备。

What does your popup looks like? Maybe it's an iFrame, that would be a logical explaination.

你的弹出窗口是什么样的?也许这是一个 iFrame,这将是一个合乎逻辑的解释。

Code:

代码:

<a class="btn btn-danger" id="clicking" data-bind="click: $root.enterLocation" onclick="alert('test')" href="#">Continue</a>

$(document).ready(function(){ 
    $(document).keyup(function(e){
        if (e.keyCode == 13){
        $("#clicking").trigger('click');       

        }
    })
});?

Better alternative is using e.which

更好的选择是使用 e.which