【发布时间】:2009-02-02 15:55:17
【问题描述】:
我想使用 jQuery 通过按 Enter 来触发超链接。我有以下 html,但 Enter-stroke 所做的只是触发警报,它不会导航到链接。但是,单击该链接会导航到新地址。我错过了什么?
我知道我的测试函数正在运行,因为 addClass/removeClass 函数正在运行。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(event){
alert(this.href);
});
});
function test(event){
if (event.keyCode == 13) {
$("a").addClass("test");
$("a.one").trigger('click');
}
else {
$("a").removeClass("test");}
}
</script>
<style type="text/css">
a.test { font-weight: bold; }
</style>
</head>
<body onkeypress='test(event)'>
<a class="one" href="http://jquery.com/">jQuery</a>
<br/>
<a class="two" href="http://stackoverflow.com/">stack overflow</a>
</body>
</html>
【问题讨论】: