回答編集履歴
2
調整
test
CHANGED
@@ -64,15 +64,19 @@
|
|
64
64
|
|
65
65
|
var node=document.querySelector('#inputArea input:last-of-type');
|
66
66
|
|
67
|
-
i
|
67
|
+
var xid=x.getAttribute('id');
|
68
68
|
|
69
|
+
var area=document.querySelector('#inputArea');
|
70
|
+
|
71
|
+
if(xid==="add" && len<5){
|
72
|
+
|
69
|
-
|
73
|
+
area.insertBefore(node.cloneNode(),node.nextSibling);
|
70
74
|
|
71
75
|
}
|
72
76
|
|
73
|
-
if(x
|
77
|
+
if(xid==="del" && len>1){
|
74
78
|
|
75
|
-
|
79
|
+
area.removeChild(node);
|
76
80
|
|
77
81
|
}
|
78
82
|
|
1
蛇足
test
CHANGED
@@ -4,9 +4,9 @@
|
|
4
4
|
|
5
5
|
<script>
|
6
6
|
|
7
|
-
window.addEventListener('DOMContentLoaded', function(
|
7
|
+
window.addEventListener('DOMContentLoaded', function(){
|
8
8
|
|
9
|
-
document.querySelector('#add').addEventListener('click',function(
|
9
|
+
document.querySelector('#add').addEventListener('click',function(){
|
10
10
|
|
11
11
|
if(document.querySelectorAll('#inputArea input').length<5){;
|
12
12
|
|
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
});
|
20
20
|
|
21
|
-
document.querySelector('#del').addEventListener('click',function(
|
21
|
+
document.querySelector('#del').addEventListener('click',function(){
|
22
22
|
|
23
23
|
if(document.querySelectorAll('#inputArea input').length>1){;
|
24
24
|
|
@@ -45,3 +45,53 @@
|
|
45
45
|
</div>
|
46
46
|
|
47
47
|
```
|
48
|
+
|
49
|
+
# 蛇足
|
50
|
+
|
51
|
+
共通部分があるのでまとめられるかなぁと思ったらイマイチ効率化できなかったソース
|
52
|
+
|
53
|
+
```javascript
|
54
|
+
|
55
|
+
<script>
|
56
|
+
|
57
|
+
window.addEventListener('DOMContentLoaded', function(){
|
58
|
+
|
59
|
+
[].forEach.call(document.querySelectorAll('#add,#del'),function(x){
|
60
|
+
|
61
|
+
x.addEventListener('click',function(){
|
62
|
+
|
63
|
+
var len=document.querySelectorAll('#inputArea input').length;
|
64
|
+
|
65
|
+
var node=document.querySelector('#inputArea input:last-of-type');
|
66
|
+
|
67
|
+
if(x.getAttribute('id')==="add" && len<5){
|
68
|
+
|
69
|
+
document.querySelector('#inputArea').insertBefore(node.cloneNode(),node.nextSibling);
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
if(x.getAttribute('id')==="del" && len>1){
|
74
|
+
|
75
|
+
document.querySelector('#inputArea').removeChild(node);
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
});
|
80
|
+
|
81
|
+
});
|
82
|
+
|
83
|
+
});
|
84
|
+
|
85
|
+
</script>
|
86
|
+
|
87
|
+
<div id="inputArea">
|
88
|
+
|
89
|
+
<input type="text" maxlength="80">
|
90
|
+
|
91
|
+
<button id="add">+</button>
|
92
|
+
|
93
|
+
<button id="del">-</button>
|
94
|
+
|
95
|
+
</div>
|
96
|
+
|
97
|
+
```
|