You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/2-events/03-event-delegation/article.md
+24-24Lines changed: 24 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -30,62 +30,62 @@ HTMLnya seperti ini:
30
30
</table>
31
31
```
32
32
33
-
The table has 9 cells, but there could be 99 or 9999, doesn't matter.
33
+
Tabel memiliki 9 sel, tapi bisa saja memiliki 99 atau 9999 sell, tidaklah penting.
34
34
35
-
**Our task is to highlight a cell`<td>`on click.**
35
+
**Tugas kita adalah untuk memberikan highlight ke sel`<td>`yang di klik.**
36
36
37
-
Instead of assign an`onclick`handler to each `<td>` (can be many) -- we'll setup the "catch-all" handler on`<table>` element.
37
+
Daripada mengatur sebuah penangan`onclick`pada setiap `<td>` (yang bisa sangat banyak) -- kita akan mengatur sebuah penangan "penangkap-semua" pada elemen`<table>`.
38
38
39
-
It will use `event.target`to get the clicked element and highlight it.
39
+
Penangan itu akan menggunakan `event.target`untuk mendapatkan elemen yang diklik dan menghighlightnya.
40
40
41
-
The code:
41
+
Kodenya:
42
42
43
43
```js
44
44
let selectedTd;
45
45
46
46
*!*
47
47
table.onclick=function(event) {
48
-
let target =event.target; //where was the click?
48
+
let target =event.target; //dimanakah klik terjadi?
49
49
50
-
if (target.tagName!='TD') return; //not on TD? Then we're not interested
50
+
if (target.tagName!='TD') return; //bukan di TD? kita tidak peduli
51
51
52
-
highlight(target); // highlight it
52
+
highlight(target); // highlight elemen itu
53
53
};
54
54
*/!*
55
55
56
56
functionhighlight(td) {
57
-
if (selectedTd) { //remove the existing highlight if any
57
+
if (selectedTd) { //hapus elemen lain yang sudah di highlight
58
58
selectedTd.classList.remove('highlight');
59
59
}
60
60
selectedTd = td;
61
-
selectedTd.classList.add('highlight'); //highlight the new td
61
+
selectedTd.classList.add('highlight'); //menghighlight elemen yang baru
62
62
}
63
63
```
64
64
65
-
Such a code doesn't care how many cells there are in the table. We can add/remove `<td>` dynamically at any time and the highlighting will still work.
65
+
Kode seperti itu, tidak peduli berapa banyak sel yang ada pada table tersebut. Kita bisa menambahkan/menghapuskan `td` secara dinamis pada waktu kapanpun dan proses menghighlight akan tetap berfungsi.
66
66
67
-
Still, there's a drawback.
67
+
Tapi, tetap ada kekurangannya.
68
68
69
-
The click may occur not on the `<td>`, but inside it.
69
+
Klik mungkin tidak terjadi pada `<td>`, tapi pada elemen didalamnya.
70
70
71
-
In our case if we take a look inside the HTML, we can see nested tags inside`<td>`, like`<strong>`:
71
+
Pada kasus kita jika dilihat pada HTML, kita memiliki sebuah elemen bersarang pada`<td>`, seperti`<strong>`:
72
72
73
73
```html
74
74
<td>
75
75
*!*
76
-
<strong>Northwest</strong>
76
+
<strong>Barat Laut</strong>
77
77
*/!*
78
78
...
79
79
</td>
80
80
```
81
81
82
-
Naturally, if a click happens on that `<strong>`then it becomes the value of`event.target`.
82
+
Biasanya, jika klik terjadi pada `<strong>`maka elemen itu akan menjadi nilai dari`event.target`.
83
83
84
84

85
85
86
-
In the handler`table.onclick`we should take such `event.target`and find out whether the click was inside `<td>`or not.
86
+
Pada penangan (_handler_)`table.onclick`kita sebaiknya mengambil `event.target`dan mencari tahu apakah klik terjadi didalam `<td>`atau tidak.
1.The method `elem.closest(selector)`returns the nearest ancestor that matches the selector. In our case we look for`<td>`on the way up from the source element.
104
-
2.If`event.target`is not inside any `<td>`, then the call returns immediately, as there's nothing to do.
105
-
3.In case of nested tables, `event.target`may be a `<td>`, but lying outside of the current table. So we check if that's actually *our table's*`<td>`.
106
-
4.And, if it's so, then highlight it.
102
+
Penjelasan:
103
+
1.Metode `elem.closest(selector)`akan mengembalikan elemen atas terdekat yang sama dengan pemilih (_selector_). Pada kasus kita yang dicari adalah`<td>`pada bagian atas dari elemen sumber.
104
+
2.Jika`event.target`tidak didalam `<td>`, maka kita akan langsung mengembalikan, karena tidak ada yang bisa dilakukan.
105
+
3.Jika pada kasus elemen bersarang didalam tabel, `event.target`bisa saja merupakan elemen `<td>`, tapi berada diluar tabel yang kita atur. Jadi kita memeriksa jika tabel itu adalah *tabel yang kita butuh*`<td>`.
106
+
4.Dan, jika benar, maka beri highlight pada elemen itu.
107
107
108
-
As the result, we have a fast, efficient highlighting code, that doesn't care about the total number of `<td>`in the table.
108
+
Hasilnya, kita memiliki kode yang cepat, efisien dalam memberikan highlight, yang tidak peduli terhadap jumlah dari elemen `<td>`pada sebuah tabel.
0 commit comments