点击定位标签链接进入新闻 [英] Click anchor tag link on enter press

查看:99
本文介绍了点击定位标签链接进入新闻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个锚标签,如

 < a class =btn btn-dangerid =点击数据-bind =点击:$ root.enterLocationhref =#>继续< / a> 

它位于弹出窗口中。我需要按回车键点击此链接。

  $(document).ready(function(){
$(document).keyup(function(event){
if(event.keyCode == 13){
$(#点击)。trigger('click');

}
})
});

不知道为什么该功能无法正常工作。我也使用点击功能也有同样的结果。它在鼠标点击时正常工作。我需要在输入新闻时自动运行。



以下代码在Firefox中正常运行。



<$ p $ (函数(){
$(document).on(keyup,function(event){
if(event.which ==)点击);点击();
}
});

});



如何在Chrome上使用这个工具? 问题在于您使用的是 event.keyCode ,并不总是在所有浏览器中使用。一些浏览器使用 event.charCode 或者甚至是一个不同的 event.which ,这可能会受到您正在使用的内容的支持。无论如何,使用jQuery从事件获取键码的正常方式是使用 event.which



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

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


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.

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

});

How to make this work on Chrome?

解决方案

I think the problem is that you're using event.keyCode, which isn't always used in all browsers. Some browsers use event.charCode or 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.

jQuery normalizes the event object 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 event parameter 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.which to get a normalized keycode for the event. http://api.jquery.com/event.which/

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

这篇关于点击定位标签链接进入新闻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆