我有一个输入字段<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不同,因为它只是获得插入符位置,而我希望设置插入符位置。
答案 0 :(得分:5)
这会将输入字段的focus
和click
事件绑定到该函数。希望这会有所帮助!
$('#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">
答案 1 :(得分:3)
您真的需要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>
答案 2 :(得分:0)
无论您要使用光标执行什么操作,都需要聚焦在字段上。
这是我使用的旧块。我什至在一个子目录中将其命名为cursor.js
。我从很多地方都拿来命名它。
这是获取/设置的光标位置。它需要一个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;
}
关于浏览器友好的注意事项,有:
.selectionStart
-Chrome / Safari / Firefox .setSelectionRange(Pos,Pos)
-Chrome / Safari / Firefox / Internet Explorer .createTextRange()
-Internet Explorer