カードになるdiv要素を50個作りそれをcardsという配列に納めております。クリックして数字を取得しそれを1カウントしをcount == 1になってその値をイベントリスナーの関数内からグローバルスコープcount0に渡してあげてcount0 == 1になると二回目はクリックイベントを発生しなくなるだろうと思いました。ですが、card[0]のクリックイベントにあえてconsole.log(count0);とコードしたところカウントが1,2,3,4,5,6と反応してしまいます。これはクリックイベントが発生しているんっでしょうか?
またどのようにコードを書けば二回目以降一回限りのイベントに出来ますか?
回答宜しくお願いします
javascript
1 2let count0 = 0; 3let count1 = 0; 4let count2 = 0; 5let count3 = 0; 6let count4 = 0; 7let count5 = 0; 8let count6 = 0; 9let count7 = 0; 10let count8 = 0; 11let count9 = 0; 12let count10 = 0; 13let count11 = 0; 14let count12 = 0; 15let count13 = 0; 16let count14 = 0; 17let count15 = 0; 18let count16 = 0; 19let count17 = 0; 20let count18 = 0; 21let count19 = 0; 22let count20 = 0; 23let count21 = 0; 24let count22 = 0; 25let count23 = 0; 26let count24 = 0; 27let count25 = 0; 28let count26 = 0; 29let count27 = 0; 30let count28 = 0; 31let count29 = 0; 32let count30 = 0; 33let count31 = 0; 34let count32 = 0; 35let count33 = 0; 36let count34 = 0; 37let count35 = 0; 38let count36 = 0; 39let count37 = 0; 40let count38 = 0; 41let count39 = 0; 42let count40 = 0; 43let count41 = 0; 44let count42 = 0; 45let count43 = 0; 46let count44 = 0; 47let count45 = 0; 48let count46 = 0; 49let count47 = 0; 50let count48 = 0; 51let count49 = 0; 52 53 54 55 56 if(count0 <= 0){ 57 cards[0].addEventListener('click',function () {console.log(count0); cards[0].textContent = wariate0; count0++; return count0; }); 58 59 } 60 if(count1 <= 0){ 61 cards[1].addEventListener('click',function () {cards[1].textContent = wariate1; count1++; return count1; }); 62 } 63 if(count2 <= 0){ 64 cards[2].addEventListener('click',function () {cards[2].textContent = wariate2; count2++; return count2; }); 65 } 66 if(count3 <= 0){ 67 cards[3].addEventListener('click',function () {cards[3].textContent = wariate3; count3++; return count3; }); 68 } 69 if(count4 <= 0){ 70 cards[4].addEventListener('click',function () {cards[4].textContent = wariate4; count4++; return count4; }); 71 } 72 if(count5 <= 0){ 73 cards[5].addEventListener('click',function () {cards[5].textContent = wariate5; count5++; return count5; }); 74 } 75 if(count6 <= 0){ 76 cards[6].addEventListener('click',function () {cards[6].textContent = wariate6; count6++; return count6; }); 77 } 78 if(count7 <= 0){ 79 cards[7].addEventListener('click',function () {cards[7].textContent = wariate7; count7++; return count7; }); 80 } 81 if(count8 <= 0){ 82 cards[8].addEventListener('click',function () {cards[8].textContent = wariate8; count8++; return count8; }); 83 } 84 if(count9 <= 0){ 85 cards[9].addEventListener('click',function () {cards[9].textContent = wariate9; count9++; return count9; }); 86 } 87 if(count10 <= 0){ 88 cards[10].addEventListener('click',function () {cards[10].textContent = wariate10; count10++; return count10; }); 89 } 90 if(count11 <= 0){ 91 cards[11].addEventListener('click',function () {cards[11].textContent = wariate11; count11++; return count11; }); 92 } 93 if(count12 <= 0){ 94 cards[12].addEventListener('click',function () {cards[12].textContent = wariate12; count12++; return count12; }); 95 } 96 if(count13 <= 0){ 97 cards[13].addEventListener('click',function () {cards[13].textContent = wariate13; count13++; return count13; }); 98 } 99 if(count14 <= 0){ 100 cards[14].addEventListener('click',function () {cards[14].textContent = wariate14; count14++; return count14; }); 101 } 102 if(count15 <= 0){ 103 cards[15].addEventListener('click',function () {cards[15].textContent = wariate15; count15++; return count15; }); 104 } 105 if(count16 <= 0){ 106 cards[16].addEventListener('click',function () {cards[16].textContent = wariate16; count16++; return count16; }); 107 } 108 if(count17 <= 0){ 109 cards[17].addEventListener('click',function () {cards[17].textContent = wariate17; count17++; return count17; }); 110 } 111 if(count18 <= 0){ 112 cards[18].addEventListener('click',function () {cards[18].textContent = wariate18; count18++; return count18; }); 113 } 114 if(count19 <= 0){ 115 cards[19].addEventListener('click',function () {cards[19].textContent = wariate19; count19++; return count19; }); 116 } 117 if(count20 <= 0){ 118 cards[20].addEventListener('click',function () {cards[20].textContent = wariate20; count20++; return count20; }); 119 } 120 if(count21 <= 0){ 121 cards[21].addEventListener('click',function () {cards[21].textContent = wariate21; count21++; return count21; }); 122 } 123 if(count22 <= 0){ 124 cards[22].addEventListener('click',function () {cards[22].textContent = wariate22; count22++; return count22; }); 125 } 126 if(count23 <= 0){ 127 cards[23].addEventListener('click',function () {cards[23].textContent = wariate23; count23++; return count23; }); 128 } 129 if(count24 <= 0){ 130 cards[24].addEventListener('click',function () {cards[24].textContent = wariate24; count24++; return count24; }); 131 } 132 if(count25 <= 0){ 133 cards[25].addEventListener('click',function () {cards[25].textContent = wariate25; count25++; return count25; }); 134 } 135 if(count26 <= 0){ 136 cards[26].addEventListener('click',function () {cards[26].textContent = wariate26; count26++; return count26; }); 137 } 138 if(count27 <= 0){ 139 cards[27].addEventListener('click',function () {cards[27].textContent = wariate27; count27++; return count27; }); 140 } 141 if(count28 <= 0){ 142 cards[28].addEventListener('click',function () {cards[28].textContent = wariate28; count28++; return count28; }); 143 } 144 if(count29 <= 0){ 145 cards[29].addEventListener('click',function () {cards[29].textContent = wariate29; count29++; return count29; }); 146 } 147 if(count30 <= 0){ 148 cards[30].addEventListener('click',function () {cards[30].textContent = wariate30; count30++; return count30; }); 149 } 150 if(count31 <= 0){ 151 cards[31].addEventListener('click',function () {cards[31].textContent = wariate31; count31++; return count31; }); 152 } 153 if(count32 <= 0){ 154 cards[32].addEventListener('click',function () {cards[32].textContent = wariate32; count32++; return count32; }); 155 } 156 if(count33 <= 0){ 157 cards[33].addEventListener('click',function () {cards[33].textContent = wariate33; count33++; return count33; }); 158 } 159 if(count34 <= 0){ 160 cards[34].addEventListener('click',function () {cards[34].textContent = wariate34; count34++; return count34; }); 161 } 162 if(count35 <= 0){ 163 cards[35].addEventListener('click',function () {cards[35].textContent = wariate35; count35++; return count35; }); 164 } 165 if(count36 <= 0){ 166 cards[36].addEventListener('click',function () {cards[36].textContent = wariate36; count36++; return count36; }); 167 } 168 if(count37 <= 0){ 169 cards[37].addEventListener('click',function () {cards[37].textContent = wariate37; count37++; return count37; }); 170 } 171 if(count38 <= 0){ 172 cards[38].addEventListener('click',function () {cards[38].textContent = wariate38; count38++; return count38; }); 173 } 174 if(count39 <= 0){ 175 cards[39].addEventListener('click',function () {cards[39].textContent = wariate39; count39++; return count39; }); 176 } 177 if(count40 <= 0){ 178 cards[40].addEventListener('click',function () {cards[40].textContent = wariate40; count40++; return count40; }); 179 } 180 if(count41 <= 0){ 181 cards[41].addEventListener('click',function () {cards[41].textContent = wariate41; count41++; return count41; }); 182 } 183 if(count42 <= 0){ 184 cards[42].addEventListener('click',function () {cards[42].textContent = wariate42; count42++; return count42; }); 185 } 186 if(count43 <= 0){ 187 cards[43].addEventListener('click',function () {cards[43].textContent = wariate43; count43++; return count43; }); 188 } 189 if(count44 <= 0){ 190 cards[44].addEventListener('click',function () {cards[44].textContent = wariate44; count44++; return count44; }); 191 } 192 if(count45 <= 0){ 193 cards[45].addEventListener('click',function () {cards[45].textContent = wariate45; count45++; return count45; }); 194 } 195 if(count46 <= 0){ 196 cards[46].addEventListener('click',function () {cards[46].textContent = wariate46; count46++; return count46; }); 197 } 198 if(count47 <= 0){ 199 cards[47].addEventListener('click',function () {cards[47].textContent = wariate47; count47++; return count47; }); 200 } 201 if(count48 <= 0){ 202 cards[48].addEventListener('click',function () {cards[48].textContent = wariate48; count48++; return count48; }); 203 } 204 if(count49 <= 0){ 205 cards[49].addEventListener('click',function () {cards[49].textContent = wariate49; count49++; return count49; }); 206 } 207 208 209 210 211
回答2件
あなたの回答
tips
プレビュー