|
1 | 1 | // Level 3
|
2 | 2 |
|
3 | 3 | /*
|
4 |
| - WooHoo, you got so far on your first day! Great! But we still have more things for you. |
5 |
| - First of all, go to index.html file and replace our script from level2.js to current |
6 |
| - file - level3.js. |
7 |
| - PS: if you want few js files to be used simply add more scripts with those files. |
| 4 | + Introduction |
| 5 | + ============ |
| 6 | +
|
| 7 | + WooHoo, you got so far on your first day! Great! |
| 8 | +
|
| 9 | + But we still have more things for you. First of all, open index.html, and |
| 10 | + replace our script from level2.js to our current file — level3.js. |
| 11 | +
|
| 12 | + P.S. If you want to use multiple js files simultaneously, simply add more |
| 13 | + script tags. |
8 | 14 | */
|
9 | 15 |
|
10 | 16 | /*
|
11 |
| - Here we will talk a bit more about HTML, CSS and how JS can interact with them. |
12 |
| - So HTML - stands for Hyper Text Markup Language. It is a structure of the elements |
13 |
| - on the page. |
14 |
| -
|
15 |
| - HTML: |
16 |
| - As you noticed it is divided on elements that looks like that: |
17 |
| - <header></header>, <p></p> etc - it is tags, each element on the page has opening |
18 |
| - and closing tag (NOTE: some tags are self-closing like <img>). Whole html file is wrapped |
19 |
| - in tag <html>, which contains of <head> and <body> tags. In <head> we keep different |
20 |
| - meta information, title and link to css files. <body> contains our actual content. |
21 |
| - Tags has a set of names which you can find here http://htmldog.com/references/html/tags/ |
22 |
| -
|
23 |
| - Any tag also can have different attributes (<div class="settings"></div> - tag div |
24 |
| - has a attribute class, that has name = 'settings'). |
25 |
| -
|
26 |
| - CSS: |
27 |
| - Stands for Cascading Style Sheets. CSS describes how HTML elements are to be |
28 |
| - displayed on screen. As you can see in css file when we need to target any element |
29 |
| - we can simply refer to the tag itself (footer{color: white;}), or to any attribute (class |
30 |
| - via '.settings', id via '#logo' etc). The list of css properties is huge, you can check |
31 |
| - on it here https://www.w3.org/TR/CSS21/propidx.html but don't worry, you don't need to |
32 |
| - remember it all. |
33 |
| - PS: difference between id and class is that tag with specific 'id' should be unique on |
34 |
| - the page, but many tags can have same class within the same page. Use 'id' only if |
35 |
| - you really need it!! |
| 17 | + Lets talk a little more about HTML, CSS, and how we can interact with them |
| 18 | + in JavaScript. |
| 19 | +
|
| 20 | +
|
| 21 | + Hypertext Markup Language (HTML) |
| 22 | + -------------------------------- |
| 23 | + As you noticed, HTML is divided on elements that looks like this: |
| 24 | +
|
| 25 | + <header></header> |
| 26 | + <p></p> |
| 27 | + etc |
| 28 | +
|
| 29 | + We call these "tags". Each element on the page has an opening and closing |
| 30 | + tag. (NOTE: some tags are self-closing like <img>). |
| 31 | +
|
| 32 | + The outermost tag in every HTML file is <html>. |
| 33 | +
|
| 34 | + Inside the <html> tag you will find a <head> and <body> tag. |
| 35 | +
|
| 36 | + In <head> we keep meta information, the page title and links to css files. |
| 37 | + <body> contains our actual content. |
| 38 | +
|
| 39 | + For a full list of HTML tags, you can refer to this listing: |
| 40 | + http://htmldog.com/references/html/tags/ |
| 41 | +
|
| 42 | + HTML tags may contain attributes: |
| 43 | + <div class="settings"></div> |
| 44 | +
|
| 45 | + This div has an attribute named class, that has value: 'settings'. |
| 46 | +
|
| 47 | +
|
| 48 | + Cascading Style Sheets (CSS) |
| 49 | + ---------------------------- |
| 50 | + CSS describes how HTML elements look. |
| 51 | +
|
| 52 | + CSS files are comprised of 'declaration blocks'. Each declaration block is |
| 53 | + composed of a selector and a set of visual style rules. A declaration looks |
| 54 | + like this: |
| 55 | +
|
| 56 | + [selector] { |
| 57 | + style-name: value; |
| 58 | + style-name: value; |
| 59 | + style-name: value; |
| 60 | + } |
| 61 | +
|
| 62 | + Selectors specify which elements the visual styles are applied to. |
| 63 | +
|
| 64 | + The most basic selectors refer to elements by their tag-name. They look |
| 65 | + like this: |
| 66 | +
|
| 67 | + body { |
| 68 | + background-color: white; |
| 69 | + } |
| 70 | +
|
| 71 | + Selectors can also refer to elements by their class attribute like this: |
| 72 | +
|
| 73 | + .settings { |
| 74 | + margin: 0; |
| 75 | + } |
| 76 | +
|
| 77 | + or IDs, like this: |
| 78 | +
|
| 79 | + #logo { |
| 80 | + text-align: center; |
| 81 | + } |
| 82 | +
|
| 83 | + The list of css properties is huge, you can read more here, if you're |
| 84 | + interested: |
| 85 | + https://www.w3.org/TR/CSS21/propidx.html |
| 86 | +
|
| 87 | + Don't worry, you don't need to remember all of this immediately! |
36 | 88 | */
|
37 | 89 |
|
38 | 90 | /*
|
39 |
| - Phew, lot's of new things. But let's come back to javaScript and see how we can interact |
40 |
| - with html. |
| 91 | + Phew, lots of new things. Let's come back to javaScript and see how we can |
| 92 | + interact with HTML. |
41 | 93 | */
|
42 | 94 |
|
43 | 95 |
|
44 | 96 | /*
|
45 |
| - Accessing page objects. |
46 |
| - DOM - Stands for Document Object Model. It defines the logical structure of documents |
47 |
| - and the way a document is accessed and manipulated. So let's get things from the page |
48 |
| - and play around. |
49 |
| - To find and get nods(page elements) we can use querySelector one of the functions of |
50 |
| - JavaScript (in old browsers it might not work, in this case getElementById, |
51 |
| - getElementsByTagsName etc should be used). |
52 |
| - Let's get our twitter from the page. |
| 97 | + Accessing Elements |
| 98 | + ================== |
| 99 | +
|
| 100 | + Document Object Model (DOM) |
| 101 | + --------------------------- |
| 102 | + The DOM is the javascript representation of the active HTML file. The DOM |
| 103 | + is available under a special global variable called 'document'. We can get |
| 104 | + specific nodes (page elements) with the DOM method: 'querySelector'. |
| 105 | +
|
| 106 | + Let's get our twitter link from the page. |
| 107 | +
|
53 | 108 | Example:
|
| 109 | +
|
54 | 110 | var ourTwitter = document.querySelector('.twitter');
|
55 |
| - //we can store it in variable so we can use it after |
| 111 | + // we can store page elements in variables, just like any other value! |
56 | 112 | */
|
57 | 113 |
|
58 |
| -//TODO: Now it's your turn - get the h1 tag from the page and store it into variable named |
59 |
| -//ourTitle. console.log it and see what you will get. |
| 114 | +// TODO: Now it's your turn — get the h1 tag from the page and store it into a |
| 115 | +// variable named ourTitle. |
| 116 | +// console.log it and see what you get! |
60 | 117 |
|
61 | 118 |
|
62 | 119 |
|
|
68 | 125 |
|
69 | 126 |
|
70 | 127 | /*
|
71 |
| - Getting a set of elements |
72 |
| - We also can get all elements with the same tag. So in the footer we have <ul>(unordered list) |
73 |
| - with 3 <li>(lists) inside. Let's get all of them |
74 |
| - Example: |
75 |
| - var mediaLinks = document.querySelectorAll('li'); //will get all <li> from the page |
| 128 | + Getting Similar Elements |
| 129 | + ======================== |
| 130 | +
|
| 131 | + We also can get all elements with the same tag. In our footer, we have an |
| 132 | + unordered list (<ul>), with three list items (<li>) inside. Let's get all |
| 133 | + of them. |
| 134 | +
|
| 135 | + Example: |
| 136 | +
|
| 137 | + // will get all <li> from the page |
| 138 | + var mediaLinks = document.querySelectorAll('li'); |
76 | 139 | */
|
77 | 140 |
|
78 | 141 |
|
79 |
| -//TODO: get all <li> elements from the page in variable named mediaLinks |
| 142 | +// TODO: Get all <li> elements from the page in variable named mediaLinks. |
80 | 143 |
|
81 | 144 |
|
82 | 145 |
|
83 | 146 |
|
84 |
| -//TODO: now console.log mediaLinks.length |
| 147 | +// TODO: Now console.log mediaLinks.length |
85 | 148 |
|
86 | 149 |
|
87 | 150 |
|
88 | 151 |
|
89 |
| -//TODO: do you still remember arrays that we had in previous sections? Using this knowledge |
90 |
| -//iterate through whole meadiaLinks items and print them out. |
| 152 | +// TODO: Do you remember loops from level 2? Using this knowledge, iterate |
| 153 | +// through each mediaLinks item and console.log it. |
91 | 154 |
|
92 | 155 |
|
93 | 156 |
|
|
98 | 161 |
|
99 | 162 |
|
100 | 163 | /*
|
101 |
| - Ok, so far so good. But what if we need only content of our 'h1' tag? We have |
102 |
| - another property for it - .innerHTML |
| 164 | + Ok, so far so good. But what if we want only the text from our 'h1' tag? |
| 165 | + Page elements have another property for this: '.textContent' |
| 166 | +
|
103 | 167 | Example:
|
104 |
| - ourTwitter.innerHTML; |
105 |
| - //we will get text '@NodeGirlsSydney' |
| 168 | +
|
| 169 | + ourTwitter.textContent; |
| 170 | + // we will get text 'Twitter: @NodeGirlsSydney @NodeGirlsMelb' |
106 | 171 | */
|
107 | 172 |
|
108 | 173 |
|
109 |
| -//TODO: get the content of 'h1' element and console.log it |
| 174 | +// TODO: get the content of 'h1' element and console.log it |
110 | 175 |
|
111 | 176 |
|
112 | 177 |
|
|
118 | 183 |
|
119 | 184 |
|
120 | 185 | /*
|
121 |
| - Change content |
122 |
| - we can change the content of the tags using the same .innerHTML property |
| 186 | + Editing Page Content |
| 187 | + ==================== |
| 188 | +
|
| 189 | + We can change the content of the tags using the same .textContent property. |
| 190 | +
|
123 | 191 | Example:
|
124 |
| - ourTwitter.innerHTML = '@ButenkoMe'; |
125 |
| - console.log(ourTwitter.innerHTML); |
126 |
| - //guess what we will see on the page and in console? |
| 192 | +
|
| 193 | + ourTwitter.textContent = '@ButenkoMe'; |
| 194 | + console.log(ourTwitter.textContent); |
| 195 | + // guess what we will see on the page and in console? |
127 | 196 | */
|
128 | 197 |
|
129 | 198 |
|
130 |
| -//TODO: change content of the 'h1' with anything you like |
| 199 | +// TODO: Make up a new title! Change the content of our 'h1' to anything you |
| 200 | +// like. |
131 | 201 |
|
132 | 202 |
|
133 | 203 |
|
134 | 204 |
|
135 | 205 |
|
136 | 206 | /*
|
137 |
| - Changing properties. |
138 |
| - We also can change and set attributes to our elements. |
139 |
| - Example: |
140 |
| - var ourTwitter = document.querySelector('.twitter'); |
141 |
| - ourTwitter.id = "surprise"; |
| 207 | + Editing Attributes |
| 208 | + ================== |
| 209 | +
|
| 210 | + We can also change and set attributes on our elements. |
| 211 | +
|
| 212 | + Example: |
| 213 | +
|
| 214 | + var ourTwitter = document.querySelector('.twitter'); |
| 215 | + ourTwitter.id = "surprise"; |
142 | 216 | */
|
143 | 217 |
|
144 | 218 |
|
145 |
| -//TODO: replace the value of 'src' attribute for our img tag with "img/kittens.jpeg" |
| 219 | +// TODO: Update the value of the 'src' attribute of our img tag to |
| 220 | +// "img/kittens.jpeg" |
146 | 221 |
|
147 | 222 |
|
148 | 223 |
|
149 | 224 |
|
150 | 225 |
|
151 | 226 | /*
|
152 |
| - Accessing and changing styles |
153 |
| - So, let's do some css changes. We can do it with help of '.style' property and |
154 |
| - giving the css property just like we do in css file, the only change here is is - if is css |
155 |
| - property name has '-' in the name (like font-size etc) then dash will be deleted and |
156 |
| - next word starts with capital (fontSize) - this way of naming is called the CamelCase :) |
| 227 | + Editing Styles |
| 228 | + ============== |
| 229 | +
|
| 230 | + Let's change some styles. Page elements have a '.style' property. We can |
| 231 | + assign styles to the style property using the same names as in CSS files. |
| 232 | +
|
| 233 | + (If a CSS property name has '-' in the name — like font-size — then the |
| 234 | + hyphen will be deleted and the next word starts with a capital instead — |
| 235 | + fontSize. This pattern of naming is called CamelCase.) |
| 236 | +
|
157 | 237 | Example:
|
| 238 | +
|
158 | 239 | var ourTwitter = document.querySelector('.twitter');
|
159 | 240 | ourTwitter.style.backgroundColor = 'white';
|
160 | 241 | */
|
161 | 242 |
|
162 | 243 |
|
163 |
| -//TODO: get any element on the page and change some styles for it |
| 244 | +// TODO: get any element on the page and change some styles for it |
164 | 245 |
|
165 | 246 |
|
166 | 247 |
|
|
169 | 250 |
|
170 | 251 |
|
171 | 252 | /*
|
172 |
| - Creating new nodes (elements) |
| 253 | + Creating New Nodes (Elements) |
| 254 | + ============================= |
| 255 | +
|
173 | 256 | The document object also provides ways to create nodes from scratch:
|
| 257 | +
|
174 | 258 | document.createElement(tagName); --> create the element
|
175 | 259 | document.createTextNode(text); --> what text it should contain
|
176 |
| - document.appendChild(); --> append it to the document |
| 260 | + document.appendChild(node); --> append it to the document |
| 261 | +
|
177 | 262 | Example:
|
| 263 | +
|
178 | 264 | var pageNode = document.querySelector('body')[0];
|
179 | 265 | var newParagraph = document.createElement('p');
|
180 | 266 | var paragraphText = document.createTextNode('Squee!');
|
|
183 | 269 | */
|
184 | 270 |
|
185 | 271 |
|
186 |
| -//TODO: Well, do you still have kittens on your screen? I like both logo and kittens. |
187 |
| -//Let's create a new image with source logo and put it into header. |
188 |
| -//PS: you also can give styles to the new node that you create. |
| 272 | +// TODO: Well, do you still have kittens on your screen? I like both logo and |
| 273 | +// kittens. Let's create a new image that sources our original logo file, and |
| 274 | +// put it into header. |
| 275 | +// |
| 276 | +// P.S. You also can give styles to the new node that you create. |
189 | 277 |
|
190 | 278 |
|
191 | 279 |
|
|
205 | 293 |
|
206 | 294 |
|
207 | 295 | ////////////////////////////////////////////////////////////////////////
|
208 |
| -//Congratulations! You have finished Part 3 of JavaScript Basics! // |
| 296 | +// Congratulations! You have finished Part 3 of JavaScript Basics! // |
209 | 297 | // Stand up, stretch your legs, celebrate your achievement. //
|
210 | 298 | // I believe you deserve on some sweets today! //
|
211 | 299 | ////////////////////////////////////////////////////////////////////////
|
|
0 commit comments