focus() jQuery 函数在 Safari 中不起作用,但在所有其他浏览器上工作正常?
我有一个搜索文本字段和搜索按钮,当在文本字段中使用默认文本或空值单击按钮时,会弹出警报并将焦点设置回搜索文本字段。这在所有主流浏览器上都运行良好,但在 safari 中则不然。
即使没有jquery,我也尝试过,但没有成功。当焦点落在搜索文本字段时,我有另一个 jQuery 函数,这就是问题所在。
将焦点设置在搜索文本上的代码是:
if (defaults.keyword == SEARCH_TIP || defaults.keyword == '') {
alert(SEARCH_NULL);
$('#store_search_keyword').focus();
return false;
}
焦点代码是:
var search_dom = $('#store_search_keyword');
var search_text = search_dom.val();
search_dom.focus(function(){
if ($(this).val() === SEARCH_TIP) {
$(this).val('');
}
});
感谢任何帮助,谢谢..
I have a search text field and search button, when button is clicked with default text in text field, or null value, an alert pops up and sets focus back on search text field. This works very well on all major browsers but not in safari.
I tried it even with out jquery, but didn't work. When the focus falls on search text field, I have another jQuery function, is that the problem.
The code that sets focus on search text is:
if (defaults.keyword == SEARCH_TIP || defaults.keyword == '') {
alert(SEARCH_NULL);
$('#store_search_keyword').focus();
return false;
}
The code on focus is:
var search_dom = $('#store_search_keyword');
var search_text = search_dom.val();
search_dom.focus(function(){
if ($(this).val() === SEARCH_TIP) {
$(this).val('');
}
});
any help is appreciated, thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该调用本机 DOM
focus()
方法,如下所示:You should call the native DOM
focus()
method, like this:这在 Safari 中无法正常工作。
http://whatsthepointy.blogspot.com/2010/ 05/safari-focus-and-popup-alerts.html
该问题是由处理程序中的“alert()”引起的。 Safari 没有意识到它的主窗口在清除警报框后重新获得焦点,并且当 Safari 认为它不是焦点应用程序时,它不会关注 上的
.focus()
调用任何东西。。That doesn't work properly in Safari.
http://whatsthepointy.blogspot.com/2010/05/safari-focus-and-popup-alerts.html
The problem is caused by that "alert()" in your handler. Safari doesn't realize that its main window regains focus after the alert box is cleared, and when Safari doesn't think it's the focused application it pays no attention to
.focus()
calls on anything.