我想要在按下某个键时单击一个链接,我编写了这个 jQuery:
$('form#form1').bind("keypress", function(e){
if(e.keycode == 13 || e.keyChar == 13 || e.which == 13){
$('.LoginBoxButton a').click();
}
});
它不起作用,我已阅读以下内容解释原因:
It's important to remember that click() will not trigger the default behavior on a link, even if nothing else is preventing it. So you can't use click() by itself to simulate the user clicking on a link and being taken to another url.
但是如何模拟用户点击链接并被带到另一个网址呢?
最佳答案
你可以这样:
$('form#form1').bind("keypress", function(e){
if(e.keycode == 13 || e.keyChar == 13 || e.which == 13){
document.location.href = $('.LoginBoxButton a').attr('href');
}
});
关于javascript - 模拟用户点击链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2664143/