teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

省略できるところがあったので消す

2021/02/26 10:24

投稿

babu_babu_baboo
babu_babu_baboo

スコア616

answer CHANGED
@@ -12,13 +12,13 @@
12
12
  </style>
13
13
 
14
14
  <body>
15
- <table border="1" id="LIST" >
15
+ <table border="1" id="LIST">
16
16
  <thead>
17
17
  <tr>
18
+ <th>No.
18
- <th>No.</th>
19
+ <th>追加/削除
19
- <th>追加/削除</th>
20
- <th>選択肢1</th>
20
+ <th>選択肢1
21
- <th>選択肢2</th>
21
+ <th>選択肢2
22
22
 
23
23
  <tbody>
24
24
  <tr>
@@ -45,7 +45,6 @@
45
45
  <option value="c1" class="c">し
46
46
  <optgroup></optgroup>
47
47
  </select>
48
- </td>
49
48
  </table>
50
49
 
51
50
 

1

寝ぼけていたので修正

2021/02/26 10:24

投稿

babu_babu_baboo
babu_babu_baboo

スコア616

answer CHANGED
@@ -3,23 +3,15 @@
3
3
 
4
4
  ```js
5
5
  <!DOCTYPE html>
6
- <html lang="ja">
6
+ <meta charset="UTF-8">
7
- <head>
8
- <title>sample</title>
7
+ <title>sample</title>
9
- <meta charset="UTF-8">
10
8
  <style>
11
- #LIST tbody tr:first-of-type { display: none; }
12
- #LIST td select optgroup:last-of-type { display: none;}
9
+ #LIST tbody tr:first-of-type, #LIST td select optgroup:last-of-type { display: none;}
13
- #LIST { counter-reset: number 0; }
10
+ #LIST { counter-reset: number 0; }
14
- #LIST tbody tr th label:after {
11
+ #LIST tbody tr th label:after { counter-increment: number 1; content: counter(number);}
15
- counter-increment: number 1;
16
- content: counter(number);
17
- }
18
12
  </style>
19
13
 
20
- </head>
14
+ <body>
21
-
22
-
23
15
  <table border="1" id="LIST" >
24
16
  <thead>
25
17
  <tr>
@@ -56,9 +48,9 @@
56
48
  </td>
57
49
  </table>
58
50
 
51
+
59
52
  <script>
60
53
 
61
-
62
54
  function replaceTRSelects (e) {
63
55
  let
64
56
  selects = [...e.closest ('tr').querySelectorAll ('select')],
@@ -86,16 +78,9 @@
86
78
  }
87
79
 
88
80
 
89
- function addRow (tr) {
81
+ function addRow (tr = null) {
90
- let
91
- [tbody, first] = tr
92
- ? tr.closest ('table').querySelectorAll ('tbody, tbody tr')
82
+ let tbody = tr ? tr.parentNode: document.querySelector ('#LIST tbody');
93
- : document.querySelectorAll ('#LIST tbody, #LIST tbody tr'),
94
- cFirst = first.cloneNode (true);
95
-
96
- (tr)
97
- ? tbody.insertBefore (cFirst, tr)
83
+ tbody.insertBefore (tbody.rows[0].cloneNode (true), tr);
98
- : tbody.appendChild (cFirst);
99
84
  }
100
85
 
101
86
 
@@ -130,5 +115,4 @@
130
115
  LIST.addEventListener ('change', handler, false);
131
116
 
132
117
  </script>
133
-
134
118
  ```