[前端] 定位光标位置

当input框获取焦点的时候,改变光标的位置

方法一:

 

function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

function setCaretToPos (input, pos) {
    setSelectionRange(input, pos, pos);
}

// 调用
setCaretToPos(document.getElementById("input"), 4);

 

 

 

方法二:

 

$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};

// 调用
$('#ele').selectRange(3,5);

 

 

 

方法三:

 

$.fn.setCursorPosition = function(position){
    if(this.lengh == 0) return this;
    return $(this).setSelection(position, position);
}

$.fn.setSelection = function(selectionStart, selectionEnd) {
    if(this.lengh == 0) return this;
    input = this[0];

    if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    } else if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }

    return this;
}

$.fn.focusEnd = function(){
    this.setCursorPosition(this.val().length);
}

// 调用
$(ele).focusEnd();

 

欢迎关注技术开发分享录:http://fenxianglu.cn/

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
前端定位功能可以通过浏览器提供的 Geolocation API 实现。该 API 可以获取用户设备的地理位置信息。 具体实现步骤如下: 1. 调用浏览器提供的 `navigator.geolocation` 对象获取地理位置信息: ``` navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options) ``` 其中,`successCallback` 是获取位置信息成功后的回调函数,`errorCallback` 是获取位置信息失败后的回调函数,`options` 是获取位置信息的一些选项参数。 2. 在 `successCallback` 回调函数中,可以获取到用户的位置信息,包括经度、纬度、精度等,可以将这些信息用于后续的功能实现。 ``` function successCallback(position) { const latitude = position.coords.latitude; const longitude = position.coords.longitude; const accuracy = position.coords.accuracy; // ... } ``` 在回调函数中,还可以使用 `GeolocationPosition` 对象提供的其他方法和属性,如 `position.coords.altitude` 获取海拔高度,`position.timestamp` 获取获取位置信息的时间戳等。 3. 在 `errorCallback` 回调函数中,可以处理获取位置信息失败的情况,如用户拒绝了位置信息的访问请求,或者浏览器无法定位用户位置等。 ``` function errorCallback(error) { switch(error.code) { case error.PERMISSION_DENIED: console.log("User denied the request for Geolocation."); break; case error.POSITION_UNAVAILABLE: console.log("Location information is unavailable."); break; case error.TIMEOUT: console.log("The request to get user location timed out."); break; case error.UNKNOWN_ERROR: console.log("An unknown error occurred."); break; } } ``` 在回调函数中,可以根据返回的 `error.code` 属性的值来判断具体的错误类型。 需要注意的是,由于涉及到用户隐私问题,浏览器会提示用户是否允许访问位置信息,因此在使用 Geolocation API 时需要特别注意用户体验和隐私保护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天空还下着雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值