溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

javascript起步

發(fā)布時(shí)間:2020-06-18 08:44:04 來(lái)源:網(wǎng)絡(luò) 閱讀:438 作者:Jasonisoft 欄目:開(kāi)發(fā)技術(shù)

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,booleanfunction

  1. 特殊數(shù)據(jù)類型:

    1. 轉(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字符

  1. 未定義值:

    1. 未定義的變量的值為undefined,表示變量還沒(méi)有賦值,或者賦予一個(gè)不存在的屬性值。但是還有其他特殊類型的數(shù)字常量NaN,,就是當(dāng)程序由于某種原因發(fā)生計(jì)算錯(cuò)誤后產(chǎn)生的一個(gè)沒(méi)有意義的值,那么這個(gè)時(shí)候javascript就返回一個(gè)NaN值。

  2. 空值:

    1. 其中有一個(gè)關(guān)鍵字null是一個(gè)特殊的值,表示為空值。其中null與undefined的區(qū)別的是:null表示一個(gè)變量被賦予了一個(gè)空值。而undefined表示該變量尚未被賦值。

  3.     typeof運(yùn)算符:typeof運(yùn)算符表示返回他的操作數(shù)的當(dāng)前所容納數(shù)的類型,這對(duì)于判斷一個(gè)變量是否已被定義有很大用處。

  4. new運(yùn)算符:通過(guò)new運(yùn)算符來(lái)創(chuàng)建一個(gè)新的對(duì)象。

    1. 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)簽。

四、

  1. 函數(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>


  1. 在事件響應(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>

  1. 通過(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>

  1. 函數(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>

  1. 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ì)象的原始值

  1. 嵌套函數(shù)的應(yīng)用

    1.   

- <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>

  1. 遞歸函數(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>

  1. 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)
按鍵鍵碼按鍵鍵碼按鍵鍵碼按鍵鍵碼
A65J74S83149
B66K75T84250
C67L76U85351
D68M77V86452
E69N78W87553
F70O79X88654
G71P80Y89755
H72Q81Z90856
I73R82048957

  


數(shù)字鍵盤(pán)上的鍵的鍵碼值(keyCode)功能鍵鍵碼值(keyCode)
按鍵鍵碼按鍵鍵碼按鍵鍵碼按鍵鍵碼
0968104F1112F7118
1979105F2113F8119
298*106F3114F9120
399+107F4115F10121
4100Enter108F5116F11122
5101-109F6117F12123
6102.110



7103/111



  


控制鍵鍵碼值(keyCode)
按鍵鍵碼按鍵鍵碼按鍵鍵碼按鍵鍵碼
BackSpace8Esc27Right Arrow39-_189
Tab9Spacebar32Dw Arrow40.>190
Clear12Page Up33Insert45/?191
Enter13Page Down34Delete46`~192
Shift16End35Num Lock144[{219
Control17Home36;:186\|220
Alt18Left Arrow37=+187]}221
Cape Lock20Up Arrow38,<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){

          //處理體

}

  1. Object對(duì)象的方法

    1. toLocaleString():該方法返回一個(gè)日期,該日期使用當(dāng)前區(qū)域設(shè)置并已被轉(zhuǎn)換為字符串。

dateObj.toLocaleString();    //dateObj表示使用一個(gè)日期類型對(duì)象

<script type="text/javascript">

    document.write(new Date().toLocaleString());

</script>

  1. String 對(duì)象

    1. 創(chuàng)建一個(gè)String 對(duì)象

var newstr = new String(String text);


    1. 比如:

var newstr = new String("hello world");

  1. String對(duì)象的屬性:

    1. length:   

- var newstr = new String("hello world");

- document.write(newstr.length);


    1. 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


向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI