我希望用户在按下回车键时转到“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 workHere 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');
更多推荐
发布评论