简体   繁体   English

动态文本框jQuery焦点

[英]dynamic textbox jquery focus

i am creating a dynamic text box of input type with the help of following code i am not able to set the focus on the input type. 我正在以下代码的帮助下创建输入类型的动态文本框,我无法将焦点设置在输入类型上。

var elem = document.createElement("input");
elem.type = "text";
elem.id = "txtParent";
elem.setAttribute('onblur', 'SetSpanValueForParent("' + spnText.id + '")');
$(elem).focus();
$(spnText).append(elem);

i have also tried doing this elem.focus(); 我也尝试过这样做elem.focus();

can u provide that one line statement how can i achieve this 您能提供一条语句吗?

You first need to append the input, before setting the focus(). 在设置focus()之前,您首先需要附加输入。

Elements can have focus only if they are visible. 元素只有在可见时才能具有焦点。

Example: 例:

function fx(spnText)
{
  var elem = document.createElement("input");
  elem.type = "text";
  elem.id = "txtParent";
  elem.setAttribute('onblur', 'SetSpanValueForParent("' + spnText.id + '")');
  $(spnText).append(elem);
  //a little delay before setting the focus
  setTimeout(function(){elem.focus()},50);
}

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

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