添加一个输入字段到DOM并将其聚焦在IE中 [英] Adding an input field to the dom and focusing it in IE

查看:117
本文介绍了添加一个输入字段到DOM并将其聚焦在IE中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个div,当你点击它变成一个输入框,并把它聚焦。我正在使用原型来实现这一点。这适用于Chrome和Firefox,但不适用于IE。即使我设置了一个1秒的超时,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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆