這是個 w3schools 提供的免費的 JS 練習題目剛好可以熟悉一些 JS 的方法,這邊我就剛好利用這個練習來整理這些用法並記錄在這篇文章中,以便未來忘記語法可以來查看
完成證明
JS Variables
第五題,他使用逗點的方式一次指派多個變數
On one single line, declare three variables with the following names and values:
firstName = “John”
lastName = “Doe”
age = 35
1 | var firstName = "John", lastName = "Doe", age = 35; |
JS Events
使用到 onmouseover
The<div>
element should turn red when someone moves the mouse over it.
1 | <div onmouseover="this.style.backgroundColor='red'">myDIV.</div> |
JS Strings
跳脫字元 利用反斜線來跳脫引號
Use escape characters to alert We are “Vikings”.
1 | var txt = "We are \"Vikings\""; |
JS String Methods
indexOf()
使用 indexOf()
找出字串內指定的字母的位置
Find the position of the character h in the string txt.
1 | var txt = "abcdefghijklm"; |
slice()
使用 slice()
從字串中切出指定的字母,(第一位字母, 最後一字母下一位)
Use the slice method to return the word “bananas”.
1 | var txt = "I can eat bananas all day"; |
replace()
使用 replace()
取代字母 (原本的字母, 取代的字母)
Use the correct String method to replace the word “Hello” with the word “Welcome”.
1 | var txt = "Hello World"; |
JS Array Methods
pop()
使用 pop()
移除最後一個內容
Use the correct Array method to remove the last item of the fruits array.
1 | var fruits = ["Banana", "Orange", "Apple"]; |
push()
使用 push()
加入內容到陣列最後
Use the correct Array method to add “Kiwi” to the fruits array.
1 | var fruits = ["Banana", "Orange", "Apple"]; |
splice()
使用 splice()
刪除指定 index 的內容
Use the splice() method to remove “Orange” and “Apple” from fruits.
1 | var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; |
sort()
使用 sort()
讓陣列內容開頭按照 abcd… 字母順序排列
Use the correct Array method to sort the fruits array alphabetically.
1 | var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; |
JS Dates
setFullYear()
使用setFullYear()
可以指定年分
Use the correct Date method to set the year of a date object to 2020.
1 | var d = new Date(); |
JS Math
Math.round()
使用 Math.round()
可以取得最接近的正整數
Use the correct Math method to round a number to the nearest integer.
1 | var x = Math.round(5.3); |
Math.sqrt()
使用 Math.sqrt()
可以取得開根號的結果
Use the correct Math method to get the square root of 9.
1 | var x = Math.sqrt(9); |
JS Switch
比較少用複習一下
如果 fruits 是 Bananas 則跳出 Hello 如果是 Apple 則跳出 Welcome
Create a switch statement that will alert “Hello” if fruits is “banana”, and “Welcome” if fruits is “apple”.
1 | switch(fruits) { |
default
如果都不是則返回 Neither
Add a section that will alert(“Neither”) if fruits is neither “banana” nor “apple”.
1 | switch(fruits) { |
JS For Loop
For…of
使用 For...of
印出陣列所有的內容
Create a loop that runs through each item in the fruits array.
1 | var fruits = ["Apple", "Banana", "Orange"]; |
JS While Loops
印出小於 10 的數字並且每次數字都會加二
Create a loop that runs as long as i is less than 10, but increase i with 2 each time.
1 | var i = 0; |
JS Break Loops
break
讓迴圈跑到 5 就停下 使用 break
Make the loop stop when i is 5.
1 | for (i = 0; i < 10; i++) { |
continue
使迴圈跳過條件內容且進入下一個迭代使用 continue
從下方的例子來理解就是跳過 5 繼續印出其他的數字
Make the loop jump to the next iteration when i is 5.
1 | for (i = 0; i < 10; i++) { |
JS HTML DOM
getElementsByTagName()
取得 HTML 內的 tag 使用getElementsByTagName()
操作方式使用[index]
選取想要操作的 tag
Use the getElementsByTagName method to find the first <p>
element, and change its text to “Hello”.
1 | <p id="demo"></p> |
getElementsByClassName()
取得指定 class 的 HTML tag 使用 getElementsByClassName()
操作方式使用[index]
選取想要操作的 tag
Change the text of the first element that has the class name “test”.
1 | <p class="test"></p> |
操作圖片的 src
操作圖片的 src
Use HTML DOM to change the value of the image’s src attribute.
1 | <img id="image" src="smiley.gif"> |
操作 input 的 value
操作 input 的 value
Use HTML DOM to change the value of the input field.
1 | <input type="text" id="myText" value="Hello"> |
操作文字顏色改變
操作文字顏色改變
Change the text color of the <p>
element to “red”.
1 | <p id="demo"></p> |
操作文字大小修改
操作文字大小修改
Change the font size of the p element to 40 pixels.
1 | <p id="demo"></p> |
操作CSS position
操作CSS position
Use the CSS display property to hide the p element.
1 | <p id="demo"></p> |
事件監聽
Use the eventListener to assign an onclick event to the <button>
element.
1 | <button id="demo">Click me1</button> |