Skip to content

Commit 96fec5f

Browse files
committed
event 03: article.md - 20%
1 parent 10635ba commit 96fec5f

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

2-ui/2-events/03-event-delegation/article.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,62 +30,62 @@ HTMLnya seperti ini:
3030
</table>
3131
```
3232

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.
3434

35-
**Our task is to highlight a cell `<td>` on click.**
35+
**Tugas kita adalah untuk memberikan highlight ke sel `<td>` yang di klik.**
3636

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>`.
3838

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.
4040

41-
The code:
41+
Kodenya:
4242

4343
```js
4444
let selectedTd;
4545

4646
*!*
4747
table.onclick = function(event) {
48-
let target = event.target; // where was the click?
48+
let target = event.target; // dimanakah klik terjadi?
4949

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
5151

52-
highlight(target); // highlight it
52+
highlight(target); // highlight elemen itu
5353
};
5454
*/!*
5555

5656
function highlight(td) {
57-
if (selectedTd) { // remove the existing highlight if any
57+
if (selectedTd) { // hapus elemen lain yang sudah di highlight
5858
selectedTd.classList.remove('highlight');
5959
}
6060
selectedTd = td;
61-
selectedTd.classList.add('highlight'); // highlight the new td
61+
selectedTd.classList.add('highlight'); // menghighlight elemen yang baru
6262
}
6363
```
6464

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.
6666

67-
Still, there's a drawback.
67+
Tapi, tetap ada kekurangannya.
6868

69-
The click may occur not on the `<td>`, but inside it.
69+
Klik mungkin tidak terjadi pada `<td>`, tapi pada elemen didalamnya.
7070

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>`:
7272

7373
```html
7474
<td>
7575
*!*
76-
<strong>Northwest</strong>
76+
<strong>Barat Laut</strong>
7777
*/!*
7878
...
7979
</td>
8080
```
8181

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`.
8383

8484
![](bagua-bubble.svg)
8585

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.
8787

88-
Here's the improved code:
88+
Ini kode yang sudah diperbaiki:
8989

9090
```js
9191
table.onclick = function(event) {
@@ -99,13 +99,13 @@ table.onclick = function(event) {
9999
};
100100
```
101101

102-
Explanations:
103-
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.
107107

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.
109109

110110
## Delegation example: actions in markup
111111

0 commit comments

Comments
 (0)