[英]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.