简体   繁体   English

将输入字段追加到其他容器后,除非再次单击,否则无法继续输入文本输入。 它失去焦点

[英]After appending input field to a different container, I cannot continue typing in the text input unless I click again. It loses focus

Here's the main part of the function that I'm having trouble with. 这是我遇到的功能的主要部分。 I am using this function on a text input field for the oninput="myFunction" attribute. 我在oninput =“ myFunction”属性的文本输入字段上使用此函数。

function myFunction() {
var myContainer1 = document.getElementById("newContainer");
var myContainer2 = document.getElementById("inputContainer");
myContainer1.appendChild(myContainer2);
myContainer2.style.display = "inline-block";
}

So what I have done here is moved my input container inside of another container. 所以我在这里所做的就是将输入容器移到另一个容器内。 After I type my first character in the input field, the field moves to the other container, however it loses focus and I cannot continue typing unless I click the input field again. 在输入字段中输入第一个字符后,该字段将移至另一个容器,但是失去焦点,除非再次单击输入字段,否则无法继续输入。

Here's an example: If you go to the google homepage: https://www.google.com/ 这是一个示例:如果您转到Google主页: https : //www.google.com/

When you start typing, the search field input element moves up to the header, however you can still continue typing as if nothing happened. 当您开始键入内容时,搜索字段输入元素将移至标题上方,但是您仍然可以继续键入内容,就好像什么都没发生一样。 The caret doesn't disappear and it doesn't force you to click the field to continue typing. 插入符号不会消失,也不会强制您单击该字段以继续输入。

My issue is, right after my input element moves, it doesn't allow me to continue typing unless I click the field again. 我的问题是,在输入元素移动后,除非再次单击该字段,否则不允许我继续输入。

I'd really appreciate any help! 我真的很感谢您的帮助!

You can fix it by requesting the focus to the desired element: 您可以通过将焦点放在所需元素上来修复它:

function myFunction() {
  var myContainer1 = document.getElementById("newContainer");
  var myContainer2 = document.getElementById("inputContainer");
  myContainer1.appendChild(myContainer2);
  myContainer2.style.display = "inline-block";
  myContainer2.focus();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在反应应用程序中输入一个字符后输入字段失去焦点 - Input field loses focus after typing one character in react app 输入字符后反应输入文本框失去焦点 - React input text box loses focus after typing a character 输入字符 React 后输入失去焦点 - Input loses focus after typing character React 如果我不按住鼠标左键单击,输入字段会失去焦点 - Input field loses focus if I don't hold down left-mouse click 文本输入失去焦点并重新获得焦点后,keypresss事件自动触发 - keypresss event automatically fires after text input loses focus and gained focus again 单击Android后,输入立即失去焦点 - Input immediately loses focus after click on Android React MUI Formik 输入在输入后失去焦点 - React MUI Formik Input loses focus after typing React-以动态形式输入1个字符后输入失去焦点 - React - Input loses focus after typing 1 char in dynamic form 在将焦点输入到下一个文本输入后 - after typing focus to next text input 使输入文本字段保持焦点而不会在我单击其他内容时丢失(焦点始终位于id =“ input_message”的输入上)? - to make that input text field holds focus and not to lose when I click on something else (to focus always be on input with id=“input_message”)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM