jquery.textinput.js
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @fileOverview jqueryui插件封装
* @author:Hbomb(zhouqq@yoho.cn)
* @date:2012-07-09
*/
define('lib/util/jquery.textinput', [ "jquery"], function(require)
{
var jQuery = require("jquery");
(function($)
{
$.fn.extend({
insertContent : function(myValue, t)
{
var $t = $(this)[0];
if (document.selection)
{// ie
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
sel.moveStart('character', -l);
var wee = sel.text.length;
if (arguments.length == 2)
{
var l = $t.value.length;
sel.moveEnd("character", wee + t);
t <= 0 ? sel.moveStart("character", wee - 2 * t
- myValue.length) : sel.moveStart(
"character", wee - t - myValue.length);
sel.select();
}
}
else if ($t.selectionStart || $t.selectionStart == '0')
{
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos)
+ myValue
+ $t.value.substring(endPos,
$t.value.length);
this.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
if (arguments.length == 2)
{
$t.setSelectionRange(startPos - t,
$t.selectionEnd + t);
this.focus();
}
}
else
{
this.value += myValue;
this.focus();
}
}
});
})(jQuery);
jQuery.fn.extend({
getCurPos : function()
{
var e = jQuery(this).get(0);
e.focus();
if (e.selectionStart)
{ // FF
return e.selectionStart;
}
if (document.selection)
{ // IE
var r = document.selection.createRange();
if (r == null)
{
return e.value.length;
}
var re = e.createTextRange();
var rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return rc.text.length;
}
return e.value.length;
},
setCurPos : function(pos)
{
var e = jQuery(this).get(0);
e.focus();
if (e.setSelectionRange)
{
e.setSelectionRange(pos, pos);
}
else if (e.createTextRange)
{
var range = e.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
},
setSelect: function(start,end)
{
var e = jQuery(this).get(0);
e.focus();
if (e.setSelectionRange)
{
e.setSelectionRange(start, end);
}
else if (e.createTextRange)
{
var range = e.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
}
});
});