jquery使用enter键导航链接(jquery navigate link with enter key)

编程入门 行业动态 更新时间:2024-10-28 21:21:18
jquery使用enter键导航链接(jquery navigate link with enter key)

我希望用户在按下回车键时转到“a href”中的链接。 我怎么做? 我已经搜索了全部,大多数建议是使用以下不起作用。 你有其他建议可以试试吗? 谢谢你的帮助。

$("a.uniq_red").triger("click"); // doesn't work $("a.uniq_red").click(); // doesn't work $("#search-results a").click(); // doesn't work

这是我到目前为止的代码:

var i=0; $('#search').keypress(function(e){ if (e.which == 13) { //$("a.uniq_red").triger(confirm('test')); //THIS WORKS!!!! $("a.uniq_red").simulate('click'); // THIS DOES NOT WORK } if (e.which >= 37 && e.which <= 40) { $('p.key-notification').fadeOut('slow'); } var keyCode = e.keyCode || e.which, arrow = {left: 37, up: 38, right: 39, down: 40 } switch (keyCode) { case arrow.down: // do something down // confirm("test"); $("#search-results a").eq(i).addClass('uniq_red'); $("#search-results a").eq(i-1).removeClass('uniq_red'); i+=1; break; case arrow.up: // do something up $("#search-results a").eq(i-2).addClass('uniq_red'); $("#search-results a").eq(i-1).removeClass('uniq_red'); i-=1; break; } });

我遵循了一个新的建议并且有效。 但我必须按两次输入键才能导致代码导航到href链接。

$("a.uniq_red").focus(); //This work, but I had to hit enter twice. $("a.uniq_red").focus().click(); //Essentially, same outcome. $("a.uniq_red").click().focus(); // also, same outcome.

I want the user to go to the link in the "a href" when they hit the enter key. How do I do that? I've searched all over and most suggestions are to use the following which doesn't work. Do you have any other suggestion that I can try? Thanks for your help.

$("a.uniq_red").triger("click"); // doesn't work $("a.uniq_red").click(); // doesn't work $("#search-results a").click(); // doesn't work

Here is my code so far:

var i=0; $('#search').keypress(function(e){ if (e.which == 13) { //$("a.uniq_red").triger(confirm('test')); //THIS WORKS!!!! $("a.uniq_red").simulate('click'); // THIS DOES NOT WORK } if (e.which >= 37 && e.which <= 40) { $('p.key-notification').fadeOut('slow'); } var keyCode = e.keyCode || e.which, arrow = {left: 37, up: 38, right: 39, down: 40 } switch (keyCode) { case arrow.down: // do something down // confirm("test"); $("#search-results a").eq(i).addClass('uniq_red'); $("#search-results a").eq(i-1).removeClass('uniq_red'); i+=1; break; case arrow.up: // do something up $("#search-results a").eq(i-2).addClass('uniq_red'); $("#search-results a").eq(i-1).removeClass('uniq_red'); i-=1; break; } });

I followed a new suggestion and it worked. But I had to hit enter key twice for the code to navigate to the a href link.

$("a.uniq_red").focus(); //This work, but I had to hit enter twice. $("a.uniq_red").focus().click(); //Essentially, same outcome. $("a.uniq_red").click().focus(); // also, same outcome.

最满意答案

如果我理解正确,你想要使用的是:

window.location = $('a.uniq_red').attr('href');

If I've understood correctly, what you want to use is:

window.location = $('a.uniq_red').attr('href');

更多推荐

本文发布于:2023-08-02 23:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   enter   jquery   key   link

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!