无法在html中对输入文本使用焦点
Unable to use focus on input text in html
我正在尝试以下代码,但它不起作用。
var option=$('<input type="text" value=""/>');
$(this).append($(option));
$(option).focus();
我还试着使用:
$(this).children().first().focus();
但这也不起作用。
以下是完整的功能:
var templatestring = "<tr><td class='itemId' style='display:none'></td><td id='tdelement' class='Correct'></td><td id='tdelement' class='Option'></td></td><td id='tdelement' class='Action'><input type='button' value='View' onclick='view(this)'/></td><td id='tdelement' class='Remove'><img src='" + STATIC_URL + "img/delete.jpg' width='20' height='20' align='center'></td></tr>";
var template = $(templatestring);
$("#addoption").button().click(function() {
var newrow = $(template).clone();
$(newrow).children().each(function() {
switch($(this).attr('class')) {
case 'itemId':
$(this).html('');
break;
case 'Option':
var option=$('<input type="text" value=""/>');
$(this).append($(option));
$(this).find('input:text').val('11');
$(option).focus();
break;
case 'Correct':
$(this).html('<input type="radio" class="Answer" name="correct"/>');
break;
case 'Action':
$('input:button', this).val('Add');
break;
}
});
$('#datatable tbody').append(newrow);
});
如果它有任何帮助,我正在使用谷歌铬。
什么是$(this)
?如果不在选择器功能中,则this
将参考窗口。如果你想附加到主体,你可以这样做
var option=$('<input type="text" value=""/>');
$('body').append($(option)); // You can also use div , selector instead of body
$(option).focus();
TD演示
HTML
<table>
<tr>
<td>Add</td>
</tr>
</table>
JS-
$('td').click(function() {
var option = $('<input type="text" value=""/>');
$(this).append($(option));
$(option).focus();
});
在OP更新后,我可以理解为什么上述解决方案不适用于您。您需要在将input
附加到table
之后调用focus
。试试这个:
var templatestring = "<tr><td class='itemId' style='display:none'></td><td id='tdelement' class='Correct'></td><td id='tdelement' class='Option'></td></td><td id='tdelement' class='Action'><input type='button' value='View' onclick='view(this)'/></td><td id='tdelement' class='Remove'><img src='" + STATIC_URL + "img/delete.jpg' width='20' height='20' align='center'></td></tr>";
var template = $(templatestring);
var option;
$("#addoption").button().click(function() {
var newrow = $(template).clone();
$(newrow).children().each(function() {
switch ($(this).attr('class')) {
case 'itemId':
$(this).html('');
break;
case 'Option':
option = $('<input type="text" value=""/>');
$(this).append($(option));
$(this).find('input:text').val('11');
$(option).focus();
break;
case 'Correct':
$(this).html('<input type="radio" class="Answer" name="correct"/>');
break;
case 'Action':
$('input:button', this).val('Add');
break;
}
});
$('#datatable tbody').append(newrow);
$(option).focus();
});
相关文章:
- 文本框焦点上的自动完成菜单
- 在文本区域的焦点上分割渐变
- 焦点输入时保持文本处于选中状态
- 无法在Chrome控制台中使用javascript将焦点和光标设置为输入文本
- 设置焦点动态创建的文本区域
- 使用 jQuery 检查文本框焦点
- 焦点新选项卡的文本区域引导程序 3 + 角度JS
- 如何在单击按钮时获得最后一个焦点文本框输入
- 将焦点设置为角度模板/路线/表单中的文本输入
- 使用jQuery在焦点上选择文本框是't在移动浏览器中工作
- 如何使用javascript将焦点设置为文本框
- 焦点事件不适用于动态生成的文本框
- 如何在隐藏的文本框上设置焦点
- OnRowEditing编辑文本框焦点
- 将焦点设置为嵌套iframe中的文本框
- 如何防止在html中模糊(焦点丢失)时取消选择所选文本
- 使用制表键时禁用只读文本框的光标焦点
- 输入文本焦点,如 Facebook 输入
- 跨浏览器输入文本焦点系统
- 当文本焦点(键盘打开)固定的标题隐藏(消失)