Javascript 基礎知識整理

來源:互聯網
上載者:User

標籤:javascript基礎

1.Javascript的作用

JavaWeb裡面 

html: 負責網頁架構,顯示內容、資料。

css: 負責樣式、格式的顯示。

javascript: 負責完成頁面的互動、網頁的行為。



2.Javascript的三種寫法


1).直接在html裡面寫。

<a href="javascript:alert(‘東方閃電‘)">超連結</a><h4 onclick="javascript:alert(111)">點擊</h4>

2).<script>標籤  可以寫在head body 裡面 或html外面。


3).使用外部檔案引入的方式:首先 建立一個js檔案 demo.js 記住:所有外部JavaScript檔案的尾碼  .js

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script type="text/javascript">        alert("Hello JavaScript!");    </script></head><body><!--javascript 寫法2:<script>標籤  可以寫在head body 裡面 或html 外面--><!--javascript 寫法3:使用外部檔案引入的方式 --><script src="demo.js"> </script></body></html><script>    alert("Hello World!");</script>



3.javascript 常用的輸入輸出方式


1).Alert("Hello!") 彈窗輸出。


2).confirm("你確定你是男的?") 選擇彈窗,返回 boolean值. 


3).prompt("請輸入:");輸入彈窗  



4.Javascript 資料類型 Javascript是一個弱語言,在使用的時候確定是什麼類型

String,number,boolean,Array,Object,null  


1).如何看你的變數是什麼類型, 使用 typeof 關鍵字。對於數組等對象而言,typeof 只會顯示object

如果要判斷是否為array, 使用 alert(as instanceof Array); 結果為true 則為array類型。


2).undefined 未定義的類型,表明你這個變數沒有被初始化或使用。undefined類型是false。


3).特別注意:在JS中除了NaN, undefined,0 這三個數是false,其餘皆是true。

可以判斷一個變數有沒有賦值。如果賦值了就是true 沒有賦值就是undefined是 false;



5.資料類型的強制轉換


1).數字轉換為字串

//             數字轉換為字串        var a=456;        alert(typeof a);        a=a.toString();//        a=456+"";        alert(typeof a);


2).字串轉換為數字

var b="11";alert(b+1);alert(Number(b)+1);


3).注意:如果強制轉換一個非數位值轉換為Number會得到一個NaN的值;

使用parseInt可以將字串開頭的幾個數字轉換為int,如果開頭不是數字,那麼只會得到NaN。

如 var b="12px";

alert(parseInt(b)); 可以得到12,用於更改字型大小等。



6.Javascript 的對象

//建立一個Person的對象function Person(name,age){this.name=name;this.age=age;this.address="四川成都";//如果沒有用this聲明,這個變數僅僅是一個局部變數,不是類的屬性。var m=10;//建立方法this.say=function(){alert(this.name+","+this.age);}}//執行個體化一個對象var p1=new Person("張飛",12);alert(p1.name);alert(p1.name+","+p1.address)alert(p1["name"]+","+p1["address"]); //也可以用alert(typeof p1);   //得到objectalert(p1 instanceof Person); //得到true //在JS中對於對象而言,可以通過for in 來獲得對象的屬性,用架構的時候可以通過它來看裡面有什麼屬性。for(var a in p1){alert(a);//alert(a+":"+p1[a]);}//函數的另一種定義方式:var x=function(){alert("x")}//此時x就是一個函數function



7.日期對象

var d=new Date();document.write(d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日"+"星期"+d.getDay())



8.字串

var str1=new String("abc");var str2="abc";alert(str1==str2);  //結果為true  沒有equalsvar s=str2.concat("hello","world");



9.數組

1).

var as=new Array();as.push(11);as.push(12);

2).

var mycars=new Array();mycars[0]="Saab";mycars[1]="Volvo";mycars[2]="BMW";

3).

var as=new Array(11,12,13,14,15,222,"15");

4).

var as=["11",12,1,2];



10.with 函數

with(document){//此時花括弧中的所有代碼都是基於document作為根對象,當使用write(xxx)就等於document.write(xxx);write();write();write();write();//使用alert也是允許的alert("abc");}



11.事件處理

<!DOCTYPE html>    <html lang="en">    <head>    <meta charset="UTF-8">    <title>Title</title>    <script>    //點擊試試    function clickd(obj){    alert(obj.innerHTML)    }    //滑鼠移過來    function moused(obj){    //設定對象的顏色,在js中設定文本的樣式均通過xx.style.樣式名稱    obj.style.color="red";    //移上來字型變大. 當使用代碼來設定樣式的時候,如果在css中通過-表示的,都有駝峰標識,font-size-->fontSize    obj.style.fontSize="18px";    }    //滑鼠移走恢複字型顏色和大小    function outd(obj){    obj.style.color="#000";    obj.style.fontSize="16px";    }    //小於5號字型時點擊變大,大於30號字型時點擊變小    var big=true;    function bigger(obj){    var cs=parseInt(obj.style.fontSize);    if(cs){    if(cs>=30){    big=false;    obj.innerHTML="點擊變小";    }    if(cs<=5){    big=true;    obj.innerHTML="點擊變大";    }    if(big){    cs+=2    }else{    cs-=2    }    obj.style.fontSize=cs+"px";    }else{    obj.style.fontSize="14px";    }    }    function cd(){    //在3秒之後會執行autoBig這個函數,setTimeout的意思就是間隔一段時間來執行某個函數,僅僅執行一次。    //如果重複執行則,用setInterval,表示每隔一段時間調用一次函數    //          setTimeout("autoBig()",1000);    setInterval("autoBig()",1000);    }    function autoBig(){    //擷取html中節點的id為txt的節點    var node=document.getElementById("txt")    var size=parseInt(node.style.fontSize);    if(size){    size+=10;    }else{    size="14";    }    node.style.fontSize=size+"px";    }    </script>    </head>    <body>    <h2>事件處理</h2>    <div onclick="clickd(this)" style="cursor:pointer"> 點擊試試</div>    <br/><br/><br/>    <div onmouseover="moused(this)" onmouseout="outd(this)">滑鼠移過來</div>    <br/><br/><br/>    <div onclick="bigger(this)" style="cursor:pointer">點擊變大</div>    <br/><br/><br/>    <div id="txt" style="cursor:pointer">開始</div>    <br/>    <div onclick="cd()" style="cursor:pointer">點擊開始操作</div>    </body>    </html>










Javascript 基礎知識整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.