javascript <a> 标签上的 onkeypress

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9653382/
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 07:21:46  来源:igfitidea点击:

onkeypress on a <a> tag

javascriptonkeypress

提问by thormayer

I'm trying get my <a>tag triggered when the user press on the "enter" key. (onkeypress).

我正在尝试<a>在用户按下“输入”键时触发我的标签。(onkeypress).

my <a>tag:

我的<a>标签:

<a href="javascript:search()" onkeypress="return runScript(event)">

this is my javascript:

这是我的javascript

 function runScript(e)
   {
        if (e.keyCode == 13) {
                alert("dssd");
        return false;

      }
   }

I dont know whats messed up ?

我不知道怎么回事?

回答by Jignesh Rajput

its work for me

它对我有用

 <a href="javascript:search();"  onkeypress="return runScript(event);">Open in new window using javascript</a>

javaScript

脚本

 window.runScript = function (e) {

       if (e.keyCode == 13) {
           alert('ss');
           return false;
       }
       else {

           return true;
       }
   }

   window.search = function () {
       alert('s');
   }

live demo: fiddle

现场演示小提琴

回答by Diode

Write your html as given below. Note the property tabindexwhich makes the a tag focusable in certain browsers.

写下你的 html,如下所示。请注意tabindex使 a 标记在某些浏览器中可聚焦的属性。

<a id="link" href="http://google.com" onkeydown="runScript(event)" tabindex="1">I am a link</a>

If you need an autofocus on load, you can use the jQuery function focusas shown below.

如果您需要在加载时自动对焦,您可以使用 jQuery 函数focus,如下所示。

$(document).ready(
    function(e){
        $("#link").focus();
    }
);

Then your function

然后你的功能

function runScript(e){
    if(e.keyCode == 13){
       alert("pressed enter key");
    }
}

you have to call e.preventDefault();(or return false in some browsers) if you want to prevent the link load the link in href.

e.preventDefault();如果要阻止链接加载 href 中的链接,则必须调用(或在某些浏览器中返回 false)。

function runScript(e){
    e.preventDefault();
    if(e.keyCode == 13){
       alert("pressed enter key");
    }
    return false;
}

see a demo here: http://jsfiddle.net/diode/hfJSn/9/showpress enter key when the page is loaded

在此处查看演示:http: //jsfiddle.net/diode/hfJSn/9/show在页面加载时按回车键

回答by Reinaldo

The ENTER key actually triggers the onclick event:

ENTER 键实际上触发了 onclick 事件:

<a href="#" onclick="alert('hello!');">

This means that your search() function inside the href will execute before the onkeypress event.

这意味着 href 中的 search() 函数将在 onkeypress 事件之前执行。

回答by JKing

That works in my browser, though I suspect it's not the way to achieve what you actually want to do... (maybe?)

这在我的浏览器中有效,尽管我怀疑这不是实现您真正想做的事情的方法......(也许?)

Number one, you probably don't want it to "return" anything, so you can just do onkeypress="runScript(e)"and it'll run. If that function does return a value, it's not gonna go anywhere...

第一,你可能不希望它“ return”做任何事情,所以你可以做onkeypress="runScript(e)",它就会运行。如果该函数确实返回一个值,它不会去任何地方......

Number two, it's kinda rare that a keydown event would fire on an anchor (<a>) element, unless of course the user tabs through the other elements 'till it has focus and then presses a key (usually the browser will "highlight" the element that currently has keyboard focus, if it's not just the whole page). Are you wanting your script to run when someone presses enter after typing in a search box or something? if so, you probably want to listen for the event on the search box itself, so add it as that element's onkeydown attribute (for example: <input id="mySearchBox" onkeydown="runScript(e)">) if you just want it to run whenever the user presses enter, regardless of focus or typing text into any particular field, just do as edmastermind29's comment said and add the event listener to the whole document.

第二,keydown 事件很少会在锚点 ( <a>) 元素上触发,除非用户在其他元素之间切换,直到它获得焦点然后按下一个键(通常浏览器会“突出显示”该元素当前有键盘焦点,如果它不仅仅是整个页面)。您是否希望您的脚本在有人在搜索框或其他内容中输入后按 Enter 键时运行?如果是这样,您可能希望在搜索框本身上侦听事件,因此将其添加为该元素的 onkeydown 属性(例如:)<input id="mySearchBox" onkeydown="runScript(e)">如果您只想在用户按下 Enter 键时运行它,而不管焦点或输入文本如何任何特定字段,只需按照 edmastermind29 的评论进行操作,并将事件侦听器添加到整个文档中。

回答by JKing

Have you tried adding this to your script?

您是否尝试将其添加到您的脚本中?

document.onkeypress = runScript;

document.onkeypress = runScript;