-
Notifications
You must be signed in to change notification settings - Fork 2
/
index_delWord.html
88 lines (78 loc) · 2.8 KB
/
index_delWord.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
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
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
<meta name="renderer" content="webkit" />
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<meta name="format-detection" content="telephone=no" />
<meta http-equiv="Expires" content="-1"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Pragma" content="no-cache"/>
<title>input 按一次del按键删除整个单词</title>
<link rel="shortcut icon" href="/favicon.ico"/>
<style type="text/css" id="style">
</style>
</head>
<body>
<header>
jackdizhu 主页
</header>
<div class="content">
<h5><span>input 一次删除整个单词</span></h5>
<div class="clearfix">
<textarea name="" id="demo" cols="30" rows="10"></textarea>
</div>
</div>
<script type="text/javascript" src="./dist/index.js"></script>
<script type="text/javascript" src="./cdn/jquery.min.js"></script>
<script type="text/javascript">
var getCursortPosition = function(ctrl) {
var CaretPos = 0;
// IE Support
if (document.selection) {
ctrl.focus();
var Sel = document.selection.createRange();
Sel.moveStart('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || +ctrl.selectionStart === 0) {
CaretPos = ctrl.selectionStart;
}
return (CaretPos);
};
var selectSomeText = function(element, begin, end) {
if (element.setSelectionRange) {
element.setSelectionRange(begin, end);
} else if (element.createTextRange) {
var range = element.createTextRange();
range.moveStart("character", begin);
range.moveEnd("character", end);
range.select();
}
};
var delWholeWord = function(text, field, pos) {
var startIndex = pos;
// 匹配正则 删除多个字符
var is = field.match(/([a-zA-Z]+)$/);
// console.log(is);
if(is){
startIndex = is.index;
}
selectSomeText(text, startIndex, pos)
};
$('#demo').keydown(function(event) {
if (event.keyCode !== 8) {
return;
}
var bodyText = $(this)[0];
var bodyField = $(this).val();
var pos = getCursortPosition(bodyText);
delWholeWord(bodyText, bodyField, pos);
});
</script>
</body>
</html>