-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask_20.html
70 lines (61 loc) · 1.63 KB
/
task_20.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>task20</title>
<style>
#result {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
#result div {
background-color: pink;
height: 30px;
line-height: 30px;
min-width: 30px;
text-align: center;
margin: 5px;
}
#result span {
background-color: red;
}
</style>
</head>
<body>
<div class="ctrl">
<textarea id="textArea" rows="5" cols="20" placeholder="please input"></textarea>
<button id="insert">插入</button>
<button id="search">查询</button>
<input id="searchInput">
</div>
<div id="result"></div>
<script>
$ = function (el) {
return document.querySelector(el);
};
var arrData = [];
$('#insert').onclick = function () {
var str = $('#textArea').value.trim();
// 分割字符串
var arrWord = str.split(/[^0-9a-zA-Z\u4e00-\u9fa5]+/).filter(function (e) {
return e.length != 0;
});
arrData = arrData.concat(arrWord);
render();
};
$('#search').onclick = function () {
var str = $('#searchInput').value.trim();
render(str);
};
function render(str) {
$('#result').innerHTML = arrData.map(function (d) {
if (str != null && str.length > 0) {
d = d.replace(new RegExp(str, "g"), "<span class='select'>" + str + "</span>");
}
return '<div>' + d + '</div>';
}).join('');
}
</script>
</body>
</html>