添加一个输入字段到DOM并将其聚焦在IE中 [英] Adding an input field to the dom and focusing it in IE
问题描述
基本上,代码是这样工作的:
var viewElement = new Element(div)。update(text);
var editElement = new Element(input,{type:text});
root.update(viewElement);
//为信息起见的快捷方式:
viewElementOnClick = function(event){$ b $ root.update(editElement);
editElement.focus();
$ b上面的例子是实际代码的缩写版,很好,除了在IE中的焦点位。
IE中的对焦功能有没有限制?我需要把输入放在一个表单中吗?
解决方案我的猜测是IE没有更新DOM,调用focus()。有时候浏览器会在更新DOM之前等到一个脚本完成执行。
我会尝试更新,然后执行
setTimeout(setFocus,0);
函数setFocus()
{
editElement.focus();
$ / code>
您的其他选项是在任何时候都将DOM只要交换style.display取决于你需要隐藏/显示在给定的时间。
I am trying to make a div, that when you click it turns into an input box, and focuses it. I am using prototype to achieve this. This works in both Chrome and Firefox, but not in IE. IE refuses to focus the newly added input field, even if I set a 1 second timeout.
Basically the code works like this:
var viewElement = new Element("div").update("text");
var editElement = new Element("input", {"type":"text"});
root.update(viewElement);
// pseudo shortcut for the sake of information:
viewElementOnClick = function(event) {
root.update(editElement);
editElement.focus();
}
The above example is a shortened version of the actual code, the actual code works fine except the focus bit in IE.
Are there limitations on the focus function in IE? Do I need to place the input in a form?
解决方案 My guess is that IE hasn't updated the DOM yet when you make the call to focus(). Sometimes browsers will wait until a script has finished executing before updating the DOM.
I would try doing the update, then doing
setTimeout("setFocus", 0);
function setFocus()
{
editElement.focus();
}
Your other option would be to have both items present in the DOM at all times and just swap the style.display on them depending on what you need hidden/shown at a given time.
这篇关于添加一个输入字段到DOM并将其聚焦在IE中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!