jQuery 将光标设置为焦点输入字段的开头

jQuery Set Cursor to Beginning of Input Field on Focus

我有一个输入字段 <input value="@website.com" type="text" name="email" id="email">,我想这样当用户关注此字段时,它会将光标移动到 @website.com.

之前

我有以下 Javascript/jQuery 代码,但它似乎不起作用:

$(document).ready(function() {
    $("#email").focusin(function(){
        $("#email").focus();
        $("#email")[0].setSelectionRange(0,0);
    });
});

似乎 .focus() 一直在调用 focusin() 函数。

我知道 $("#email")[0].setSelectionRange(0,0); 是将光标移动到前面的正确方法,但是当字段获得焦点时如何将其绑定到?

编辑:所以当我使用时:

$("#email").focus(function(){
    $("#email")[0].setSelectionRange(0,0);
});

它在我跳入该字段时将光标设置到开头,但在我单击时却没有。

Edit2:这与 enter link description here 不同,因为它只是获取插入符位置,而我正在寻找设置插入符位置。

这会将输入字段的 focusclick 事件绑定到该函数。希望这对您有所帮助!

$('#email').on('focus click', function() {
  $(this)[0].setSelectionRange(0, 0);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" placeholder="Enter your name" autofocus>
<input type="text" id="email" value="@website.com">

或者,没有 jQuery...

document.getElementById('email').addEventListener('click', moveCursor);
document.getElementById('email').addEventListener('focus', moveCursor);

function moveCursor(event) {
   event.target.setSelectionRange(0, 0);
}
<input type="text" placeholder="Enter your name" autofocus>
<input type="text" id="email" value="@website.com">

你真的需要 jQuery 吗?

document.getElementById("email").addEventListener("click", moveCursor);
document.getElementById("email").addEventListener("focus", moveCursor);

function moveCursor(event) {
    event.target.setSelectionRange(0,0);
}
<input value="@website.com" type="text" name="email" id="email"></input>

无论您想用光标执行什么,都需要一个字段焦点。

这是我使用的旧块。我什至在子目录中将它命名为 cursor.js...我猜是从很多地方拿来的。

这是一个get/set光标位置。它需要一个 JS 元素。 .oO($(el)[0] 语法松散..)

var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
    // Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== 'undefined';   // Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
    // At least Safari 3+: "[object HTMLElementConstructor]"
var isChrome = !!window.chrome && !isOpera;              // Chrome 1+
var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6


function GetCursorPos (field) {

  // Initialize
  iCaretPos = 0;

  if (isIE){

    // Set focus on the element
    field.focus();

    // To get cursor position, get empty selection range
    oSel = field.createTextRange();

    // Move selection start to 0 position
    oSel.moveStart('character', -field.value.length);

    // The caret position is selection length
    iCaretPos = oSel.text.length;
  }

  if (isChrome||isSafari){
    iCaretPos = field.selectionStart;
  }

  if (isFirefox){
    iCaretPos = field.selectionStart;
  }

  return iCaretPos;
}

function SetCursorPos (field,Pos) {

  // Set focus on the element
    field.focus();

  if (isIE){
    field.selectionStart=Pos;
  }

  if (isChrome||isSafari){
    field.setSelectionRange(Pos,Pos);
  }

  if (isFirefox){
    field.selectionStart=Pos;
  }

  return;
}

关于浏览器友好的考虑,有:

  1. .selectionStart -- Chrome/Safari/Firefox
  2. .setSelectionRange(Pos,Pos) -- Chrome/Safari/Firefox/Internet Explorer
  3. .createTextRange() -- Internet Explorer


我很久以前在那里使用的灵感:partial (compared to the above) SO answer