-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_script_hints.html
More file actions
151 lines (131 loc) · 3.7 KB
/
Copy pathjava_script_hints.html
File metadata and controls
151 lines (131 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Paweł Adamski</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="stylesheets/normalize.css" media="screen">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen">
</head>
<body>
<section class="page-header">
<h1 class="project-name">Java Script - ściągawka</h1>
</section>
<section class="main-content">
<h2>Wyszukiwanie elementów</h2>
Pamiętaj, że konsola programisty (F12) podpowiada, wszystkie poniższe nazwy.
<ul>
<li> document.getElementById - zwraca wskazany element
<li> document.getElementsByClassName - zwraca tablicę elementów
<li> document.getElementsByTagName - zwraca tablicę elementów
</ul>
<h2>Atrybuty elementów</h2>
Każdy element strony ma swoje atrybuty. Najważniejsze z nich to:
<ul>
<li>className</li>
<li>style
<li>innerHTML
<li>value
</ul>
Każdy z tych elementów, można zarówno odczytywać jak i zapisywać.
<h2>Jak dodać obsługę zdarzenia klinięcia myszki</h2>
Dodaj atrybut/zdarzenie <strong>onclick</strong> do elementu i przekarz sterowanie do funkcji.
<pre>
<button id="liczba" onclick="<strong>dodaj()</strong>"/>
<script>
function <strong>dodaj()</strong> {
// ...
}
</script>
</pre>
<h2>Jak stworzyć funkcje</h2>
<pre>
<script>
function dodaj() {
// ...
}
</script>
</pre>
<h2>Jak używać funkcji</h2>
<pre>
<script>
function dodaj() {
// ...
}
<strong> dodaj()</strong>
</script>
</pre>
<h2>Funkcja z parametrami</h2>
<pre>
<script>
//Definicja
function poleProstokatu(a,b) {
return a*b;
}
// Użycie
poleProstokatu(5,8);
</script>
</pre>
<h2>Pobieranie wartości pola tekstowego</h2>
<pre>
<input id="<strong>liczba</strong>"/>
<script>
var liczbaText = document.getElementById("<strong>liczba</strong>").value;
var liczba = parseInt(liczbaText);
</script>
</pre>
<h2>Jak wstawić kod HTML na stronę</h2>
<pre>
<div id="<strong>pole</strong>"/>
<script>
document.getElementById("<strong>pole</strong>").innerHTML = "Moj kod";
</script>
</pre>
<h2>Jak zmienić klasę elementu</h2>
<pre>
<div id="<strong>pole</strong>"/>
<script>
document.getElementById("<strong>pole</strong>").className = "pogrugiony";
</script>
</pre>
<h2>Jak odczytać klasę elementu</h2>
<pre>
<div id="<strong>pole</strong>"/>
<script>
var klasa = document.getElementById("<strong>pole</strong>").className
alert("Element pole ma klase: "+klasa);
</script>
</pre>
<h2>Pętla while</h2>
Schemat:
<pre>
while (<warunek>) {
<kod>
}
</pre>
Przykład:
<pre>
<script>
var i = 0;
while (i<10) {
alert(i);
i++;
}
</script>
</pre>
<h2> Warunki (if-y)</h2>
<pre>
<script>
var wiek = parseInt(prompt("Podaj wiek"));
if (wiek>=18) {
alert("Możesz wejść.");
} else {
alert("Jesteś za młody.");
}
</script>
</pre>
</section>
</body>
</html>