Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery focus on textbox在使用警报时设置光标,但在没有警报时不设置光标_Jquery_Cursor_Focus_Set_Alert - Fatal编程技术网

jQuery focus on textbox在使用警报时设置光标,但在没有警报时不设置光标

jQuery focus on textbox在使用警报时设置光标,但在没有警报时不设置光标,jquery,cursor,focus,set,alert,Jquery,Cursor,Focus,Set,Alert,我在对元素使用focus()时遇到了一些奇怪的问题。 我需要将光标设置在单击的位置。我可以获得单击的位置,但我有点困惑,因为我无法在focus()事件中将其设置为该位置。 这是我的代码: function modify(value, item_ID, attr_NAME, obj_name) { var item_id = item_ID; var attr_name = attr_NAME; $('.ajax').html($('.ajax input').val())

我在对元素使用focus()时遇到了一些奇怪的问题。 我需要将光标设置在单击的位置。我可以获得单击的位置,但我有点困惑,因为我无法在focus()事件中将其设置为该位置。 这是我的代码:

function modify(value, item_ID, attr_NAME, obj_name) {
    var item_id = item_ID;
    var attr_name = attr_NAME;
    $('.ajax').html($('.ajax input').val());
    $('.ajax').removeClass('ajax');
    $(value).addClass('ajax');
    $(value).html(
            '<input id="editbox"' + ' type="text" value="' + $(value).text()
                    + '"/>');


    $('#editbox').focus(function(e) {
        var pos = GetCaretPosition(this);
        alert(pos);
    });


    $('#editbox').keydown(function(event) {
        if (event.which == 13) {
            $(document).attr('helpAttr', false);
            confirmAction(this.value, item_id, attr_name, obj_name);
        }
    });
    $('#editbox').blur(
            function() {
                if ($(document).attr('helpAttr')
                        || ($(document).attr('helpAttr') == undefined)) {
                    confirmAction(this.value, item_id, attr_name, obj_name);
                } else {
                    $(document).attr('helpAttr', true);
                }
            });
}

//Got this from the Internet
function GetCaretPosition(ctrl) {
    var CaretPos = 0; 
// IE Support
    if (document.selection) {
        ctrl.focus();
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
        CaretPos = ctrl.selectionStart;
    return (CaretPos);
}
函数修改(值、项目ID、属性名称、对象名称){
var item_id=item_id;
var attr_name=attr_name;
$('.ajax').html($('.ajax输入').val());
$('.ajax').removeClass('ajax');
$(value).addClass('ajax');
$(value).html(
'');
$('#editbox')。焦点(函数(e){
var pos=GetCaretPosition(此);
警报(pos);
});
$('#editbox').keydown(函数(事件){
if(event.which==13){
$(document.attr('helpAttr',false);
确认(this.value,item_id,attr_name,obj_name);
}
});
$(“#编辑框”).blur(
函数(){
if($(document.attr('helpAttr'))
||($(document.attr('helpAttr')==未定义)){
确认(this.value,item_id,attr_name,obj_name);
}否则{
$(document.attr('helpAttr',true);
}
});
}
//这是从网上得到的
函数GetCaretPosition(ctrl){
var-CaretPos=0;
//IE支持
if(文档选择){
ctrl.focus();
var Sel=document.selection.createRange();
Sel.moveStart('character',-ctrl.value.length);
CaretPos=Sel.text.length;
}
//Firefox支持
else if(ctrl.selectionStart | | ctrl.selectionStart=='0')
CaretPos=ctrl.selectionStart;
返回(CaretPos);
}
奇怪的是,如果我使用alert(pos),光标将被放置在这个位置。 是否有任何解决方案可以删除警报,因为每次单击文本框都会收到消息框会造成中断。
谢谢

这可能会有帮助:效果不错,但很糟糕。有时候我可以设定,有时候不行。如果我把光标放在某个地方->如果我只移动文本框并释放鼠标按钮,它就可以工作了。。。但这并不是我真正想要的:-(