【发布时间】:2012-07-26 06:27:41
【问题描述】:
我有以下代码可以正常工作:
// click on the button
$(".btnHeaderSearch").click(function(event)
{
event.preventDefault();
window.open('/search.aspx?phrase=' + $('.txtHeaderSearch').val());
});
它将在新窗口/选项卡中打开 search.aspx,并且该窗口/选项卡将具有焦点或位于所有其他窗口/选项卡的前面。
但是,下面的代码不起作用:
// press enter key when textbox has focus to simulate button click:
$('.txtHeaderSearch').bind('keyup', function(e) {
if ( e.keyCode === 13 ) { // 13 is enter key
window.open('/search.aspx?phrase=' + $('.txtHeaderSearch').val());
}
});
这里发生的情况是窗口/选项卡确实打开了,但它在所有其他窗口后面打开了一个窗口,或者它打开了一个选项卡而不给选项卡焦点,所以我最终留在了原始页面上。
如何按下回车键来打开一个窗口/选项卡,其行为方式与上面的按钮单击相同?即我希望这两个代码位都可以打开一个窗口或选项卡,该窗口或选项卡立即具有焦点,就像当前单击一样。
我在 IE8 中遇到了这种行为。它在谷歌浏览器中运行良好,我没有在任何其他浏览器上尝试过。不幸的是,我需要让它在 IE8 中工作。
【问题讨论】:
标签: javascript jquery internet-explorer-8 jquery-selectors jquery-events