[英]Why does jquery focus() fire only once?
I have a Chrome Extension that adds an iframe to specific URLs. 我有一个Chrome扩展程序,可将iframe添加到特定的网址。
I created a shortcut key to move focus to an input box in the iFrame. 我创建了一个快捷键,用于将焦点移至iFrame中的输入框。
The content script sends a message to background, which messages the iframe. 内容脚本将消息发送到后台,该消息向iframe发送消息。
It works great - once. 它很棒-一次。
I've read some old threads about focus() only working once on Chrome...I can't believe that to be true. 我已经阅读了一些有关focus()的旧线程,这些线程只能在Chrome上运行一次...我不敢相信这是真的。 I think it's due to some sort of event doubling or something...
我认为这是由于某种事件加倍或某种原因造成的。
Any ideas? 有任何想法吗?
Here is the message receiver in the iframe JS. 这是iframe JS中的消息接收器。 Again, this ALWAYS receives the message to place focus on the input.
同样,此命令始终会收到消息以将焦点放在输入上。 It just doesn't do it after the first call.
只是在第一个电话之后不这样做。 Refreshing the page resets things and the messaging will work once.
刷新页面会重置内容,并且消息传递将运行一次。
Thank you! 谢谢!
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.method === "focusToolbar"){
console.log('Receiving message from background to focus toolbar in aSearch.js...');
// $('#searchLibraryTB').on("focus", function(){
// console.log('search already had focus...');
// // $('#searchLibraryTB').val("this sux");
// $('#searchLibraryTB').off( "focus" );
// // $('#searchLibraryTB').focus();
// });
$('#searchLibraryTB').focus();
}
});
Sometimes writing out your problem in StackOverflow helps you think through it... 有时在StackOverflow中写出您的问题可以帮助您仔细思考...
I added a blur event before the focus...to make sure the element does NOT have focus on second and subsequent messaging... 我在焦点之前添加了一个模糊事件...以确保该元素不会将焦点放在第二个和后续消息上...
This did the trick. 这成功了。
$('#searchLibraryTB').blur();
$('#searchLibraryTB').focus();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.