-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtt.html
126 lines (106 loc) · 3.27 KB
/
tt.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
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
125
126
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test test</title>
<style type="text/css">
body,html{
padding: 0;
margin: 0;
font-size: 14px;
color: #000000;
}
table{
border-collapse: collapse;
width: 100%;
table-layout: fixed;
}
thead{
background: #3d444c;
color: #ffffff;
}
td,th{
border: 1px solid #e1e1e1;
padding: 0;
height: 30px;
line-height: 30px;
text-align: center;
}
</style>
</head>
<body>
<table id="jsTrolley">
<thead><tr><th>名称</th><th>价格</th><th>操作</th></tr></thead>
<tbody>
<!-- <tr><td>产品1</td><td>10.00</td><td><a>删除</a></td></tr>
<tr><td>产品2</td><td>30.20</td><td><a>删除</a></td></tr>
<tr><td>产品3</td><td>20.50</td><td><a>删除</a></td></tr> -->
</tbody>
<tfoot><tr><th>总计</th><td colspan="2">60.70(3件商品)</td></tr></tfoot>
</table>
<script type="text/javascript">
void function add(items) {
items.map(v => v.price = v.price.toFixed(2));
items.forEach(item => {
var doc = document.createElement('tr');
doc.innerHTML = `<tr><td>${item.name}</td><td>${item.price}</td><td><a href="#">删除</a></td></tr>`;
var tbody = document.querySelector('#jsTrolley tbody');
tbody.appendChild(doc);
})
}([{name: '商品1',price: 10},{name: '商品2',price: 30.2},{name: '商品3',price: 20.5}])
void function bind() {
var tbody = document.querySelector('#jsTrolley tbody');
var tfoot = document.querySelector('#jsTrolley tfoot td');
tbody.addEventListener('click', function(e) {
var target = e.target;
if(target.nodeName === 'A' && target.innerText === '删除') {
e.preventDefault();
tbody.removeChild(target.parentNode.parentNode);
var num = parseInt(tfoot.innerText.substr(tfoot.innerText.indexOf('(')+1))-1;
tfoot.innerText = (parseFloat(tfoot.innerText) - Number(target.parentNode.previousSibling.innerText)).toFixed(2) + `(${num}件商品)`
}
}, false)
}()
// function calc(arr) {
// let sum = 0;
// let tmp = 0;
// for(let i=0,len=arr.length;i<len;i++) {
// if (arr[i] === '(' && arr[i+1] === '(') {
// sum++;
// tmp++;
// } else if (arr[i] === '(' && arr[i-1] === '(') {
// sum++;
// } else {
// i += tmp;
// tmp = 0;
// }
// }
// let res = sum;
// while(--sum) {
// res *= sum;
// }
// return res||1;
// }
// console.log(calc('()(())(())'),calc('(())()(())'),calc('(())(())()'), calc('(((())))'))
function calc2(str) {
const replacer = 'A';
str = str.replace(/\(\(/g, replacer);
let arr = str.split('');
// if (arr.every(v => {return v !== replacer})) {
// return 1
// }
let sum = 0;
arr.forEach((v, i) => {
if (v === replacer && (arr[i-1] === '(' || arr[i+1] === '(')) {
sum += 3;
} else if (v === replacer) {
sum += 2;
}
});
return sum;
}
console.log(calc2('()()()'),calc2('(((())))'),calc2('()'),calc2('(()())'))
console.log(calc2(''), calc2('(()()(()))'))
</script>
</body>
</html>