2011-08-17 22 views
1
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

    <head> 
     <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
     <meta name="author" content="bbbbb" /> 

     <title>Untitled 2</title> 
     <script type="text/javascript" src="js/jquery-1.5.1.js"></script> 

    <style> 
    #test ul{list-style:none;} 
    #test li{width:75px;height:25px;padding-left:25px;padding-right:25px;padding-top:10px;padding-bottom:10px;} 
    .active{background:#AEABAB;} 
    a.active{color:#ffffff;} 
    </style> 
    </head> 

    <body> 


<script type="text/javascript"> 
$(document).ready(function() 
{ 
     $(document).keydown(function(e) { 
    switch(e.keyCode) { 
    case 38: // up arrow 
    window.location.href = 'index.html'; 
    break; 
    } 
    }); 
}); 
</script> 
<div id="test"> 
<ul> 
    <li class="active"><a href="test.html">Home</a></li> 
    <li><a href="#">My work</a></li> 
    <li><a href="#">Blog</a></li> 
    <li><a href="#">News</a></li> 
</ul> 
</div> 
</body> 
</html> 

我不想使用"window.location.href = 'index.html';"但想单击我的导航链接使用单击方法是可能的?如何使用jquery进行键盘导航?


you can check here

我创建了一个页面键盘导航!

+1

最有可能的浏览器将阻止使用'。点击()'导航到一个链接。请参阅http://stackoverflow.com/questions/6927215/trigger-javascript-in-href-attribute-with-jquery-click-or-similar –

+0

[jKey](http://oscargodson.com/labs/jkey/)是一个开始的好地方。 – Kayla

回答

0

$('#ID').click();不针对href中,但如果你已经定义onclick它的目标是。

所以

$(document).ready(function() { 
    $(document).keydown(function(e) {   
    switch(e.keyCode) { 
    case 38: // up arrow 
     $('#work').click(); 
     break; 
    } 
    });  
}); 

将执行<a href="#" id="work" onclick="alert('test')">Test</a>警报。

http://jsfiddle.net/2SKAH/6/

+0

如果你想链接它的某个地方,你可以添加的onclick = “location.href = 'url.html';”它http://jsfiddle.net/2SKAH/7/ –

+0

检查它的解决方案在这里.... http://www.instatutorial.com/jquery-keyboard-navigation – teacher

0
$('html a:first').trigger('click'); 
0

是,给每一个链路的ID或类别及用途:

case 38: //Up arrow 
$('#home').click(); 

这将导致与的ID的链接被点击。

+0

http://www.instatutorial.com/jquery-keyboard-navigation – teacher

1

是的,如果你要点击“激活”链接,然后可以更换window.location.href =“的index.html”有:

window.location.href = $("li.active a").attr('href'); 

如果你只是想映射键38到链接,那么你应该给锚的ID(indexAnchor),所以

<a id="indexAnchor" href="text.html">Home</a> 

window.location.href = $("#indexAnchor").attr('href'); 

Here是为例即

+0

实际上点击不在任何链接工作,我已经试过这种方法... – teacher

+0

这种方法没有响应...并没有重定向到URL。 .. – teacher

+0

你是对的。我已经更新了我的答案。 –