简体   繁体   English

为什么jquery focus()仅触发一次?

[英]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. 它很棒-一次。

  • The console message in the code below (which is the in the iframe js file) always fires - so I know the message is getting through 以下代码(即iframe js文件中的代码)中的控制台消息始终会触发-因此我知道消息正在通过
  • If I change the code to insert a value into the input it works every time 如果我更改代码以在输入中插入值,则每次都可以使用
  • I added $('#searchLibraryTB').blur(); 我添加了$('#searchLibraryTB')。blur(); to the success handler for the input box...this seems to clear things out and subsequent messages to focus on the input will work. 到输入框的成功处理程序...这似乎可以清除所有内容,随后专注于输入的消息将起作用。 It is only if you focus on the input, then mouse away (without entering anything and triggering the success handler) that the focus does not work. 只有将焦点放在输入上,然后将鼠标移开(不输入任何内容并触发成功处理程序),焦点才会起作用。 Through a little trial and error I think the issue is that I set the focus once, and the iframe thinks the element STILL has focus - even after I've clicked back into the main window. 经过一番尝试和错误,我认为问题是我只设置了一次焦点,而iframe认为STILL元素具有焦点-即使在我单击回主窗口之后也是如此。 So this path works because I programmatically blur away from the input. 因为我以编程方式模糊了输入的内容,所以该路径有效。 So the solution has to do something to clear/remove the focus set from the first iteration. 因此,该解决方案必须做一些事情来清除/删除第一次迭代中的焦点集。
    • I have an "on focus clear any value' line in the iframe JS file and, while the cursor doesn't stay in the input when the message comes through it does clear out any text in the input, implying something is getting through. 我在iframe JS文件中有一条“重点清除任何值”行,并且当消息通过时光标没有停留在输入中,但它确实清除了输入中的任何文本,这意味着正在通过。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM