hof*_*lie 2 html javascript jquery focus
我在表格单元格中有一堆文本输入,如下所示:
<td class="tdTextInput">
<input type="text" value="0" name="txt1_9_4_2" id="txt1_9_4_2" class="input-supermini">
</td>
Run Code Online (Sandbox Code Playgroud)
每当用户点击单元格或输入时,它必须自动选择输入内的所有内容(有点像电子表格编辑器).
所以这里的脚本到目前为止只能在可靠的旧Firefox中成功实现.
//focus the textbox on td click
$('.tdTextInput').mousedown(function ()
{
$(this).find('input').first().focus();
});
//select all text on focus
$('.tdTextInput input').focus(function ()
{
//the data-selected attribute is used to prevent the
// autoselection to happen more than once per cell so that
// two consecutive clicks will allow the user to pinpoint the
// cursor to a specific position
var isSelected = $(this).attr('data-selected') == 'true';
if (!isSelected) {
$('input[data-selected]').removeAttr('data-selected');
$(this).attr('data-selected', 'true');
$(this).select();
}
});
//prevent non-numeric values from being added
$('.tdTextInput input').keydown(function (e)
{
CommonTools.IsNumeric(e);
});
Run Code Online (Sandbox Code Playgroud)
CommonTools.IsNumeric指的是:- (可能不相关,因为keydown函数不是问题.只在问题中添加它才能完整)
isNumeric = function (e)
{
if(!(e.which>=48 && e.which<=57)) //numeric values only
e.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)
为什么这只适用于FF和IE而不适用于Chrome?
更新: 我在这里创建了一个小提琴:http://jsfiddle.net/dDc73/,但它甚至不适用于FF或IE中的小提琴.
更多信息:当我单击单元格时,它会选择所有文本,直到我释放鼠标单击.
参考: 使用jQuery在Safari和Chrome中无法选择焦点上的文本
$(".tdTextInput input").mouseup(function(e){
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
这也可能有所帮助:
$(".tdTextInput input").live('mouseup', function () {
$(this).select();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6732 次 |
| 最近记录: |