標籤:select 程式碼片段 控制 sel math 輸入 html 關鍵字 計時
<!DOCTYPE html><html><head><meta charset="utf-8"><title></title></head><body><p id = ‘box‘></p><script>//1.定義函數有幾種方式 /* 1、調用關鍵字來構造 function sum(x,y){ return x+y; } 2、使用Funtion()建構函式 var f = new Function( " x " , " return x*x; " ); console.log(f(2)); 3、函數直接量聲明函數 var f = function (x) {reurn x * x}; */ //2.retrun關鍵字的意義 /* 1、返回控制與函數結果 return 運算式; 在函數語句借宿時執行,並返回運算式的值作為函數的結果 2、返回控制 返回空值,文法return: false; 阻止預設事件 3、中斷函數執行 *///3.函數是否可以重定義,會有什麼事情發生/* 不能重定義,後面會覆蓋前面*///4.var和function關鍵字有什麼特別功能? /* 變數提前聲明 *///5.如何理解變數的範圍?/* 能夠使用函數的位置,分為局部範圍,全域範圍, (1)最外層函數和在最外層函數外面定義的變數擁有全域範圍 (2)所有末定義直接賦值的變數自動聲明為擁有全域範圍 (3)所有window對象的屬性擁有全域範圍 和全域範圍相反,局部範圍一般只在固定的程式碼片段內可訪問到,最常見的例如函數內部,所有在一些地方也會看到有人把這種範圍稱為函數範圍 *///6.請定義一個函數,該函數功能為,傳入一個值,返回這個值的平方值(就是這個值相乘的結果)/*function getSquare(x){return x*x;}console.log("getSquare(10):"+getSquare(10));*///7.請寫出一個函數,功能為:傳入三個值,則返回這三個值最大的那個值/*function getMax(a,b,c){var max = a;if(b>a){max = b;}if(c>max){max = c;}return max;}console.log(getMax(1132,212,1));*///8.請寫出一個函數,功能為:傳入二個值,分別是x和y,返回x的y次方結果 /* function getPow(x,y){ return Math.pow(x,y); } console.log("getPow:"+getPow(2,10));*///9.請定義一個函數,輸入一個N值,則計算出該值的階乘,即:n! = 1*2*3*4*5*6*...*N /* function getFactorial(x){ if(x==0){ return 1; } else{ return x*getFactorial(x-1); } } console.log("getFactorial:"+getFactorial(5));*///10.類比一個長按事件(提示,用定時器實現)//提示/* div.addEventListener("mousedown",function(){alert("滑鼠左鍵點下去觸發")})div.addEventListener("mouseup",function(){ alert("滑鼠左鍵彈上來時觸發")})*/ /* document.write("<div><h1>按我</h1></div>"); var div = document.querySelector("div"); div.addEventListener("mousedown",function(){ setTimeout(console.log("滑鼠左鍵點下去觸發"),2000); })div.addEventListener("mouseup",function(){ setTimeout(console.log("滑鼠左鍵彈上來時觸發"),2000);})*///11.類比一個雙擊事件(提示,用定時器實現) /* function getTime(){ var date = new Date(); return date.getTime(); } var t = 0; var f1; var f2; var interval = 300; window.addEventListener("click",function(){ t++; if(t==1){ console.log("1"); f1 = getTime(); } if(t==2){ console.log("2"); f2 =getTime(); console.log(f2-f1); if(f2-f1<interval){ alert("dbclick!"); } t=0; } })*///12.實現一個任意時間的倒計時。(提高題)//提示,擷取當前的時間戳記 (new Date()).getTime()/* var box = document.querySelector("#box"); setInterval(setVal,1000); var t = "2017,8,25"; var ts = t.split(",") var date2 = new Date(t); function setVal(){ var date1 = new Date(); var interval = parseInt(parseInt(date2.getTime())-parseInt(date1.getTime())); var day = parseInt(interval/1000/60/60/24); var hour = parseInt((interval-day*1000*60*60*24)/60/60/1000); var minute = parseInt((interval-day*1000*60*60*24-hour*60*60*1000)/60/1000); var second = parseInt((interval-day*24*60*60*1000-hour*60*60*1000-minute*60*1000)/(1000)%(60*60)); var s = "距離"+ts[0]+"年"+ts[1]+"月"+ts[2]+"日"+"還有"+day+"天"+hour+"時"+minute+"分"+second+"秒"; box.innerHTML = s; }*/</script></body></html>
Javascript---- 練習五