|
1 | | -# Searching: getElement*, querySelector* |
| 1 | +# جستجو: *getElement و *querySelector |
2 | 2 |
|
3 | | -DOM navigation properties are great when elements are close to each other. What if they are not? How to get an arbitrary element of the page? |
| 3 | +وقتی elementها به یکدیگر نزدیک هستند، استفاده از ویژگیهای DOM navigation عالی است. اما اگر elementها نزدیک هم نباشند چه؟ چگونه باید به یک element دلخواه دسترسی داشته باشیم؟ |
4 | 4 |
|
5 | | -There are additional searching methods for that. |
6 | 5 |
|
7 | | -## document.getElementById or just id |
| 6 | +روشهای جستجوی دیگری برای آن وجود دارد. |
8 | 7 |
|
9 | | -If an element has the `id` attribute, we can get the element using the method `document.getElementById(id)`, no matter where it is. |
| 8 | +## document.getElementById یا تنها id |
10 | 9 |
|
11 | | -For instance: |
| 10 | +اگر یک element،دارای attribute (ویژگی) id باشد، میتوانیم با استفاده از methodای به نام `document.getElementById(id)` به آن دسترسی داشته باشیم. اهمیتی ندارد که آن element کجا است. |
| 11 | + |
| 12 | +برای مثال: |
12 | 13 |
|
13 | 14 | ```html run |
14 | 15 | <div id="elem"> |
15 | 16 | <div id="elem-content">Element</div> |
16 | 17 | </div> |
17 | 18 |
|
18 | 19 | <script> |
19 | | - // get the element |
| 20 | + // دسترسی به element |
20 | 21 | *!* |
21 | 22 | let elem = document.getElementById('elem'); |
22 | 23 | */!* |
23 | 24 |
|
24 | | - // make its background red |
| 25 | + // پسزمینهی آن را قرمز میکنیم |
25 | 26 | elem.style.background = 'red'; |
26 | 27 | </script> |
27 | 28 | ``` |
28 | 29 |
|
29 | | -Also, there's a global variable named by `id` that references the element: |
| 30 | +همچنین، متغیری global وجود دارد که با `id` نامگذازی شده است و به element ارجاع میدهد: |
30 | 31 |
|
31 | 32 | ```html run |
32 | 33 | <div id="*!*elem*/!*"> |
33 | 34 | <div id="*!*elem-content*/!*">Element</div> |
34 | 35 | </div> |
35 | 36 |
|
36 | 37 | <script> |
37 | | - // elem is a reference to DOM-element with id="elem" |
| 38 | + // است elem به نام id با DOM-element ارجاعی به elem |
38 | 39 | elem.style.background = 'red'; |
39 | 40 |
|
40 | 41 | // id="elem-content" has a hyphen inside, so it can't be a variable name |
|
0 commit comments