入門
1:javascript是什么?
JavaScript是作為一個(gè)合格的程序員必須要掌握的知識(shí), 是互聯(lián)網(wǎng)上最流行的腳本語言,這門語言可用于 HTML 和 web,更可廣泛用于服務(wù)器、PC、筆記本電腦、平板電腦和智能手機(jī)等設(shè)備。
JavaScript 是腳本語言
JavaScript 是一種輕量級的編程語言。
JavaScript 是可插入 HTML 頁面的編程代碼。
JavaScript 插入 HTML 頁面后,可由所有的現(xiàn)代瀏覽器執(zhí)行。
JavaScript 很容易學(xué)習(xí)。
2:怎么使用Javascript
在HTML中插入<script></script>來執(zhí)行Javascript的操作
基礎(chǔ)實(shí)例:
<script>
var a='我是a';
document.write(a)? 會(huì)在頁面上輸出我是a;
alert(a);會(huì)在頁面上彈出a;
console.log(a);會(huì)在控制臺(tái)輸出;
都比較常用的方法
</script>
在事件中的使用
事件
onclick;onmouseup;onmousedown;onmousemove;ontouchstart;ontouchmove;ontouchend;
<script>
<button type="button" onclick="alert('歡迎!')">點(diǎn)我!</button>
在頁面上彈出一個(gè)歡迎
</script>
onclick 事件只是您即將在本教程中學(xué)到的眾多事件之一。
3:獲取變量
document.getElementById("id")
document.getElementByClassName("classname")
document.queryseletor('''')
document.queryseletorAll('''')
javascript改變css樣式
var x=document.getElementById("demo") //找到元素?
x.style.color="#ff0000";
4:for循環(huán)遍歷語句
for?- 循環(huán)代碼塊一定的次數(shù)
for(var i=1;? i<100;i++){
console.log(i)
遍歷1--100以內(nèi)的數(shù)
}
for/in?- 循環(huán)遍歷對象的屬性
var person={fname:"John",lname:"Doe",age:25};
for (x in person)? // x 為屬性名{? ? txt=txt + person[x];}
while?- 當(dāng)指定的條件為 true 時(shí)循環(huán)指定的代碼塊
while (i<5){ x=x + "The number is " + i + "<br>";
? ? i++;}
do/while?- 同樣當(dāng)指定的條件為 true 時(shí)循環(huán)指定的代碼塊
do{ x=x + "The number is " + i + "<br>";
? ? i++;}while (i<5);
5:條件判斷語句
if(){
執(zhí)行代碼塊 1? ? ? ? break;
}else{
? ? 執(zhí)行代碼塊 2? ? ? ? break;
}
switch(n){ case 1:
? ? ? ? 執(zhí)行代碼塊 1? ? ? ? break;
? ? case 2:
? ? ? ? 執(zhí)行代碼塊 2? ? ? ? break;
? ? default:
? ? ? ? 與 case 1 和 case 2 不同時(shí)執(zhí)行的代碼}
他們都可以用來判斷很多的操作;