jQuery:获取输入选择范围 [英] jQuery: get input selection range

查看:69
本文介绍了jQuery:获取输入选择范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery中是否有任何事件可以监听输入选择?

例如,假设我已经在文本输入中写了foo_bar.然后,我用鼠标选择foo.因此,该事件应返回0, 2(foo的字符串位置).

有什么可能的方法来做到这一点?我不知道从哪里开始,我正在翻阅麻烦的jQuery API页面,但似乎找不到任何有用的东西.

我虽然要监听输入中的mousedown事件,然后以某种方式检索发生单击的字符串的位置(不确定是否可能),然后获取发生mouseup的位置.

任何信息,来源或示例都将受到欢迎.

解决方案

$('#foo').select(function(e) {
    var start = e.target.selectionStart;
    var end = e.target.selectionEnd;
    console.log('Changed: ' + start + ' to ' + end);
});

演示: http://jsfiddle.net/ZyDjV/1/


您可以为此使用 select 事件:

$('#foo').select(function(e) {
   console.log('Selection Changed');
});

演示: http://jsfiddle.net/ZyDjV/

Is there any event in jQuery to listen to input selections?

For example, let's say I've written foo_bar in a text input. I then select the word foo with the mouse. So the event should return 0, 2 (the string position of foo).

Is there any possible way to accomplish this? I don't know where to start, I'm flipping trought jQuery API pages but can't seems to find anything helpfull.

I though about listening to mousedown events on the input, then somehow retrieve the position of the string where the click happened (not sure if it's even possible) and then getting the position where the mouseup happened.

Any info, sources or examples will be welcomed.

解决方案

Edit:

$('#foo').select(function(e) {
    var start = e.target.selectionStart;
    var end = e.target.selectionEnd;
    console.log('Changed: ' + start + ' to ' + end);
});

Demo: http://jsfiddle.net/ZyDjV/1/


You can use the select event for this:

$('#foo').select(function(e) {
   console.log('Selection Changed');
});

Demo: http://jsfiddle.net/ZyDjV/

这篇关于jQuery:获取输入选择范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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