如何在html中按F2打开链接或按钮?

时间:2014-02-18 05:04:01

标签: javascript html

这是我的代码

<a href="https://www.google.co.in">Google.com</a>

我希望在按F2或任何功能键时打开...

4 个答案:

答案 0 :(得分:1)

document.onkeydown = function(){
if(window.event && window.event.keyCode == 113) 
{
   window.location.href = "http://www.google.com"
}
}

答案 1 :(得分:0)

在这里,您可以使用jquery实现您的需求:jsfiddle.net/96rLf/2。

主要部分是:jsfiddle.net/96rLf/2. 你可以使用jquery

<input type="button" accesskey="?" value="Next Item">

$(window).keydown(function(e) {
    switch (e.keyCode) {
        case 37: case 38:  //key is left or up
            if (currImage <= 1) {break;} //if is the first one do nothing
            goToPrev(); // function which goes to previous image
            return false; //"return false" will avoid further events
        case 39: case 40: //key is left or down
            if (currImage >= maxImages) {break;} //if is the last one do nothing
            goToNext(); // function which goes to next image
            return false; //"return false" will avoid further events
    }
    return; //using "return" other attached events will execute
});

答案 2 :(得分:0)

您可以使用onkeydown方法。

 document.onkeydown = function(){
 if(window.event && window.event.keyCode == 113) 
 {
    window.location.href = "http://www.yoursite.com"
 }
}

在这里你可以使用任何东西而不是113.F2是113是ascii.F1是112,F3是114,依此类推。

答案 3 :(得分:0)

<html> 
<head> 
    <script> 
        document.addEventListener("keydown", keyDown, false);

        function keyDown(e) 
        {
            var keyCode = e.keyCode;
            if(keyCode==113) {window.location.href = "http://www.google.com";}
        }
    </script> 
</head>
<body>
    <a href="https://www.google.co.in">Google.com</a> 
</body>
</html>