使用jquery获取keyup位置 [英] Get keyup position using jquery

查看:77
本文介绍了使用jquery获取keyup位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

如何在textarea中获取插入符号位置?


如果我在html textarea控件的任何地方输入*,我需要获取keyup事件的当前位置,如Welcome * to jQuery。所以我有*后欢迎意味着在第8位。让我知道是否有人可以帮助我。

解决方案

这会起作用。 (注意:引号是7时的8位)

  $(#tf)。on('keyup', function(){
console.log($(this).val()。indexOf('*'));
});

http://jsfiddle.net/Vandeplas/ hc6ZH /



更新:多个解决方案*

<$ p $ ('keyup',function(){
var pos = [],
lastOc = 0,
p = $($#$) (this).val()。indexOf('*',lastOc);

while(p!== -1){
pos.push(p);
lastOc = p +1;
p = $(this).val()。indexOf('*',lastOc);
}
console.log(pos);
} );

http://jsfiddle.net/Vandeplas/hc6ZH/1/



更新: 仅给出您刚输入的* char的位置

 (function($,undefined){
$ .fn.getCursorPosition = function(){
var el = $(this).get(0) ;
var pos = 0;
if('elStart'in el){
pos = el.selectionStart;
} else if('document'in document){
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange()。text.length;
Sel.moveStart('character',-el.value.length);
pos = Sel.text.length - SelLength;
}
返回pos;
}
})(jQuery); ('keypress',function(e){
var key = String.fromCharCode(e.which);
if(key)

$(#tf)。 ==='*'){
var position = $(this).getCursorPosition();
console.log(position);
} else {
return false;
}
});

Possible Duplicate:
How to get caret position in textarea?

If i type * in anywhere in the html textarea control i need to get the current position on keyup event like "Welcome* to jQuery". So i have * after Welcome means at 8th position. Let me know if anybody can help me on this.

解决方案

This will work. (note: with quotes it's at 8 else at 7)

$("#tf").on('keyup', function(){
    console.log($(this).val().indexOf('*'));
});​

http://jsfiddle.net/Vandeplas/hc6ZH/

UPDATE: solution with multiple *

$("#tf").on('keyup', function(){
    var pos = [],
        lastOc = 0,
        p = $(this).val().indexOf('*',lastOc);

    while( p !== -1){
        pos.push(p);
        lastOc = p +1;
        p = $(this).val().indexOf('*',lastOc);
    }
    console.log(pos);
});​

http://jsfiddle.net/Vandeplas/hc6ZH/1/

UPDATE: giving only the position of the * char you just typed

(function ($, undefined) {
    $.fn.getCursorPosition = function() {
        var el = $(this).get(0);
        var pos = 0;
        if('selectionStart' in el) {
            pos = el.selectionStart;
        } else if('selection' in document) {
            el.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -el.value.length);
            pos = Sel.text.length - SelLength;
        }
        return pos;
    }
})(jQuery);

$("#tf").on('keypress', function(e){
    var key = String.fromCharCode(e.which);
    if(key === '*') {
        var position = $(this).getCursorPosition();
        console.log(position);
    } else {
        return false;
    }
});​

http://jsfiddle.net/Vandeplas/esDTj/1/

这篇关于使用jquery获取keyup位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆