您好,登錄后才能下訂單哦!
Javascript
定義一個(gè)變量:var x=3; 或者 x=3;
判斷一個(gè)數(shù)據(jù)的類型:
var x= 9;
var c ='d';
alert(typeof(x)=="number");
alert(typeof(c)=="char");
//結(jié)果為true
五種基本數(shù)據(jù)類型
- typeof 4; //null
- typeof 'string'; //String
- typeof null; //object
- typeof []; //object
- typeof e //undefined
- typeof true //boolean
- typeof (function(){}) //function
所以javascript有5種基本數(shù)據(jù)類型:number,string ,undefined,boolean和function
特殊數(shù)據(jù)類型:
轉(zhuǎn)義字符:
轉(zhuǎn)義字符 | 說(shuō)明 | 轉(zhuǎn)義字符 | 說(shuō)明 |
\b | 退格 | \v | 跳格(Tab) |
\n | 回車換行 | \r | 換行 |
\t | Tab符號(hào) | \\ | 反斜杠 |
\f | 換頁(yè) | \OOO | 八進(jìn)制整數(shù),范圍為000~777 |
\' | 單引號(hào) | \xHH | 十六進(jìn)制整數(shù),范圍00~FF |
\” | 雙引號(hào) | \uhhhh | 十六進(jìn)制編碼的Unicode字符 |
未定義值:
未定義的變量的值為undefined,表示變量還沒(méi)有賦值,或者賦予一個(gè)不存在的屬性值。但是還有其他特殊類型的數(shù)字常量NaN,,就是當(dāng)程序由于某種原因發(fā)生計(jì)算錯(cuò)誤后產(chǎn)生的一個(gè)沒(méi)有意義的值,那么這個(gè)時(shí)候javascript就返回一個(gè)NaN值。
空值:
其中有一個(gè)關(guān)鍵字null是一個(gè)特殊的值,表示為空值。其中null與undefined的區(qū)別的是:null表示一個(gè)變量被賦予了一個(gè)空值。而undefined表示該變量尚未被賦值。
typeof運(yùn)算符:typeof運(yùn)算符表示返回他的操作數(shù)的當(dāng)前所容納數(shù)的類型,這對(duì)于判斷一個(gè)變量是否已被定義有很大用處。
new運(yùn)算符:通過(guò)new運(yùn)算符來(lái)創(chuàng)建一個(gè)新的對(duì)象。
new constructor[(argument)]
二:String 對(duì)象提供了一個(gè)轉(zhuǎn)換為小寫(xiě)的方法:
toLowerCase();
- var a='AFFS';
- var b=a.toLowerCase();
document.write(b); // 結(jié)果為:affs
三,if......else
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="content-http" content="text/html" charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
function countdown(title,Intime,divId){
var online=new Date(Intime);
var now = new Date();
var leave = online.getTime()-now.getTime();
var day = Math.floor((leave/1000/60/60/24))+1;
if(day>1){
divId.innerHTML="<b>---距"+title+"還有"+day+"天!</b>";
}
else if(day==1){
divId.innerHTML="<b>-------tomorrow is "+title+"啦!</b>";
}
else if(day==0){
divId.innerHTML="<b>-------today is "+title+"啦!</b>";
}
else{
divId.innerHTML="<b>-------Uh!"+title+"has been gonne!</b>";
}
}
</script>
</head>
<body>
<table width="350" height="450" border="0" align="center" cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="350" height="418" border="0" cellspacing="0" cellpadding="0" >
<tr>
<td width="76"></td>
<td width="270">
<div id="countDown">
<b>-------</b>
</div>
<script type="text/javascript">
countdown("2017圣誕節(jié)","12/25/2017",countDown);
</script>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
//.innerHTML表示在哪個(gè)標(biāo)簽里嵌入html標(biāo)簽。
四、
函數(shù)的簡(jiǎn)單的響應(yīng):
- <!doctype html>
- <html lang="en">
- <head>
- <meta http-equiv="content-type" content="text/html" charset="UTF-8">
- <title>Document</title>
- <script type="text/javascript">
- function functionName(string){
- alert(string);
- }
- </script>
- </head>
- <body>
- <script type="text/javascript">
- functionName("intersting!");
- </script>
- </body>
- </html>
在事件響應(yīng)里調(diào)用函數(shù):通過(guò)點(diǎn)擊按鈕觸發(fā)來(lái)調(diào)用函數(shù)
- <!doctype html>
- <html lang="en">
- <head>
- <meta http-equiv="content-type" content="text/html" charset="UTF-8">
- <title>Document</title>
- <script type="text/javascript">
- function functionName(string){
- alert(string);
- }
- </script>
- </head>
- <body>
- <form name="form" method="post" action="">
- <input type="submit" value="提交" />
- </form>
- </body>
- </html>
通過(guò)鏈接來(lái)調(diào)用函數(shù)
- <!doctype html>
- <html lang="en">
- <head>
- <meta http-equiv="content-type" content="text/html" charset="UTF-8">
- <title>Document</title>
- <script type="text/javascript">
- function functionName(){
- var string ="intersting!";
- alert(string);
- }
- </script>
- </head>
- <body>
- <a href="javascript:functionName();" >this is a test</a>
- </body>
- </html>
函數(shù)返回值的使用
- <!doctype html>
- <html lang="en">
- <head>
- <meta http-equiv="content-type" content="text/html" charset="UTF-8">
- <title>Document</title>
- <script type="text/javascript">
- function functionName(num1,num2,num3){
- document.write("running numbers ..........</br>");
- var num = parseInt(avg(num1,num2,num3)); //parseInt表示取整數(shù)部分
- document.write("The avger result of caculate number is "+num);
- }
- function avg(num1,num2,num3){
- return (num1+num2+num3)/3;
- }
- </script>
- </head>
- <body>
- <script type="text/javascript">
- functionName(52,12,30);
- </script>
- </body>
- </html>
javascript的一些Math函數(shù)
1.丟棄小數(shù)部分,保留整數(shù)部分
parseInt(5/2)
2.向上取整,有小數(shù)就整數(shù)部分加1
Math.ceil(5/2)
3,四舍五入.
Math.round(5/2)
4,向下取整
Math.floor(5/2)
Math 對(duì)象的方法
FF: Firefox, N: Netscape, IE: Internet Explorer
方法 描述 FF N IE
abs(x) 返回?cái)?shù)的絕對(duì)值 1 2 3
acos(x) 返回?cái)?shù)的反余弦值 1 2 3
asin(x) 返回?cái)?shù)的反正弦值 1 2 3
atan(x) 以介于 -PI/2 與 PI/2 弧度之間的數(shù)值來(lái)返回 x 的反正切值 1 2 3
atan2(y,x) 返回從 x 軸到點(diǎn) (x,y) 的角度(介于 -PI/2 與 PI/2 弧度之間) 1 2 3
ceil(x) 對(duì)一個(gè)數(shù)進(jìn)行上舍入。 1 2 3
cos(x) 返回?cái)?shù)的余弦 1 2 3
exp(x) 返回 e 的指數(shù)。 1 2 3
floor(x) 對(duì)一個(gè)數(shù)進(jìn)行下舍入。 1 2 3
log(x) 返回?cái)?shù)的自然對(duì)數(shù)(底為e) 1 2 3
max(x,y) 返回 x 和 y 中的最高值 1 2 3
min(x,y) 返回 x 和 y 中的最低值 1 2 3
pow(x,y) 返回 x 的 y 次冪 1 2 3
random() 返回 0 ~ 1 之間的隨機(jī)數(shù) 1 2 3
round(x) 把一個(gè)數(shù)四舍五入為最接近的整數(shù) 1 2 3
sin(x) 返回?cái)?shù)的正弦 1 2 3
sqrt(x) 返回?cái)?shù)的平方根 1 2 3
tan(x) 返回一個(gè)角的正切 1 2 3
toSource() 代表對(duì)象的源代碼 1 4 -
valueOf() 返回一個(gè) Math 對(duì)象的原始值
嵌套函數(shù)的應(yīng)用
- <script type="text/javascript">
- var outter =10;
- function add(num1,num2){
- function inneradd(){
- alert("參數(shù)的和為:"+(num1+num2+outter));
- }
- return inneradd(); //返回嵌套函數(shù)的值
- }
- </script>
<script type="text/javascript">
add(10,20);
</script>
遞歸函數(shù)
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html" charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
function f(num){
if(num==1){
return num;
}
else{
return f(num-1)*num;
}
}
</script>
</head>
<body>
<script type="text/javascript">
document.write("10的階乘為:"+f(10));
</script>
</body>
</html>
JavaScript的內(nèi)置函數(shù)
函數(shù) | 說(shuō)明 |
eval() | 求字符串中表達(dá)式的值 |
isFinite() | 判斷一個(gè)值是否為無(wú)窮大 |
isNaN() | 判斷一個(gè)值是否是NaN |
parseInt() | 將字符串轉(zhuǎn)換為整型 |
parseFloat() | 將字符串轉(zhuǎn)換為浮點(diǎn)型 |
encodeURI() | 將字符串轉(zhuǎn)換為有效的URL地址 |
encodeURIComponent() | 將字符串轉(zhuǎn)換為有效的URL組件 |
decodeURI() | 對(duì)encodeURI編碼的文本進(jìn)行解碼 |
decodeURIComponent() | 對(duì)encodeURIComponent()編碼的文本進(jìn)行解碼 |
Keycode對(duì)照表
字母和數(shù)字鍵的鍵碼值(keyCode) | |||||||
按鍵 | 鍵碼 | 按鍵 | 鍵碼 | 按鍵 | 鍵碼 | 按鍵 | 鍵碼 |
A | 65 | J | 74 | S | 83 | 1 | 49 |
B | 66 | K | 75 | T | 84 | 2 | 50 |
C | 67 | L | 76 | U | 85 | 3 | 51 |
D | 68 | M | 77 | V | 86 | 4 | 52 |
E | 69 | N | 78 | W | 87 | 5 | 53 |
F | 70 | O | 79 | X | 88 | 6 | 54 |
G | 71 | P | 80 | Y | 89 | 7 | 55 |
H | 72 | Q | 81 | Z | 90 | 8 | 56 |
I | 73 | R | 82 | 0 | 48 | 9 | 57 |
數(shù)字鍵盤(pán)上的鍵的鍵碼值(keyCode) | 功能鍵鍵碼值(keyCode) | ||||||
按鍵 | 鍵碼 | 按鍵 | 鍵碼 | 按鍵 | 鍵碼 | 按鍵 | 鍵碼 |
0 | 96 | 8 | 104 | F1 | 112 | F7 | 118 |
1 | 97 | 9 | 105 | F2 | 113 | F8 | 119 |
2 | 98 | * | 106 | F3 | 114 | F9 | 120 |
3 | 99 | + | 107 | F4 | 115 | F10 | 121 |
4 | 100 | Enter | 108 | F5 | 116 | F11 | 122 |
5 | 101 | - | 109 | F6 | 117 | F12 | 123 |
6 | 102 | . | 110 | ||||
7 | 103 | / | 111 |
控制鍵鍵碼值(keyCode) | |||||||
按鍵 | 鍵碼 | 按鍵 | 鍵碼 | 按鍵 | 鍵碼 | 按鍵 | 鍵碼 |
BackSpace | 8 | Esc | 27 | Right Arrow | 39 | -_ | 189 |
Tab | 9 | Spacebar | 32 | Dw Arrow | 40 | .> | 190 |
Clear | 12 | Page Up | 33 | Insert | 45 | /? | 191 |
Enter | 13 | Page Down | 34 | Delete | 46 | `~ | 192 |
Shift | 16 | End | 35 | Num Lock | 144 | [{ | 219 |
Control | 17 | Home | 36 | ;: | 186 | \| | 220 |
Alt | 18 | Left Arrow | 37 | =+ | 187 | ]} | 221 |
Cape Lock | 20 | Up Arrow | 38 | ,< | 188 | '" | 222 |
多媒體鍵碼值(keyCode) | |||||||
按鍵 | 鍵碼 | ||||||
音量加 | 175 | ||||||
音量減 | 174 | ||||||
停止 | 179 | ||||||
靜音 | 173 | ||||||
瀏覽器 | 172 | ||||||
郵件 | 180 | ||||||
搜索 | 170 |
五、Javascript內(nèi)部對(duì)象
object對(duì)象的屬性:
1).prototype:該屬性返回對(duì)象類型的引用。
<script type="text/javascript">
function array_max(){
var i,max =this[0];
for (i =1;i<this.length;i++){
if(max<this[i]){
max=this[i];
}
}
return max;
}
Array.prototype.max=array_max;
var x = new Array(1,2,3,9,5,6);
var y = x.max();
document.write(y);
</script>
2).constructor屬性:
x= new String("HI");
if(x.constructor==String){
//處理函數(shù)體
}
或
function MyFunc(){
//函數(shù)體
}
y = new MyFunc;
if(y.constructor==MyFunc){
//處理體
}
Object對(duì)象的方法
toLocaleString():該方法返回一個(gè)日期,該日期使用當(dāng)前區(qū)域設(shè)置并已被轉(zhuǎn)換為字符串。
dateObj.toLocaleString(); //dateObj表示使用一個(gè)日期類型對(duì)象
<script type="text/javascript">
document.write(new Date().toLocaleString());
</script>
String 對(duì)象
創(chuàng)建一個(gè)String 對(duì)象
var newstr = new String(String text);
比如:
var newstr = new String("hello world");
String對(duì)象的屬性:
length:
- var newstr = new String("hello world");
- document.write(newstr.length);
prototype屬性:
StringObj.prototype.name =value;
- <script type="text/javascript">
- function personnal(name,age){
- this.name=name;
- this.age=age;
- }
- var information = new personnal("張三",25);
- personnal.prototype.salay = null;
- information.salay=3000;
- document.write("年齡為"+information.age+"的"+information.name+"的薪水為"+information.salay);
</script>
未完待續(xù).....................
可加本人印象筆記賬號(hào):1468359547@qq.com
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。