-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.html
37 lines (34 loc) · 1.05 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="jquery.caret.js"></script>
<script type="text/javascript">
$(function() {
var element = $('#text');
var text = element.val();
element.caret(2);
var pos = element.caret();
element.val(text.substring(0, pos) + 'b' + text.substring(pos));
element = $('#editable');
text = element.html();
element.caret(1);
pos = element.caret();
element.html(text.substring(0, pos) + 'b' + text.substring(pos));
$('#click_test').click(function(){
var input = $(this).html('<input type="text" value="test text">').find('input');
input.focus().caret(-1);
});
});
</script>
</head>
<body>
<div id="click_test">Click me</div>
<textarea id="text">a
c</textarea>
<input type="text" id="pos">
<button onclick="$('#pos').val($('#text').caret())">read</button>
<button onclick="$('#text').caret($('#pos').val())">write</button>
<div id="editable" contenteditable="true">ac</div>
</body>
</html>