三、JS基礎

來源:互聯網
上載者:User

標籤:集合   isp   bsp   ntb   使用者   car   style   代碼   seo   

 三、js基礎語句1、JS調用,可以放在任意地方,建議放在html末端(1)調用js檔案<script type="css/javascript" src="js/web.js"></script>(2)直接編寫<script type="css/javascript" >alert("hello");</script>2、變數控制語句a、變數定義,弱類型語言,沒有變數類型var d = new Date();var a = 1 ;var m = 0 , n = 0 ;var cars =["1","2","3"]; //結構如下var names=["張三","李四"];for(var i =0;i<names.length;i++ ){if (names[i]=="張三"){alert(names[i]);} }b、定義方法和調用方法:(1)不帶參數的方法sum(); //方法調用function sum(){//定義方法var n=0,sum=0;while(n<10){sum=sum+n;n++}alert ("為"+ sum);}(2)帶參數的方法sum(80,100); //方法調用function sum(n,m){//定義方法var n,sum=0;while(n<m){sum=sum+n;n++}alert ("為"+ sum);}(3)帶參和傳回值的方法al(3);//調用加成之和function pro(m){//定義方法,求m! m的階乘var n=1,pro=1;while(n<=m){pro=pro*n;n++}return pro; //傳回值} function al(n){//階乘之和var sum=0;for (i=1; i <=n;i++){sum=sum +pro(i)}alert(sum);}c、事件,事件寫在標籤的屬性裡,""內填寫js方法名,""內的字串加‘‘onclick=""//單擊事件ondblclick=""//雙擊事件onload=""//載入時間,一般放在body屬性上onfocus=""//聚集焦點onblur=""//失去焦點onmousedown=""//滑鼠點下onmouseover=""//滑鼠懸浮onmouseout=""//滑鼠移開 d、js輸入輸出輸入:prompt();輸出:alert("你好");doucument.write("你好"); e.Document對象方法(HTML DOM Style對象)close()//關閉用 document.open() 方法開啟的輸出資料流,並顯示選定的資料。getElementById()//返回對擁有指定 id 的第一個對象的引用。getElementsByName()//返回帶有指定名稱的對象集合。getElementsByTagName()//返回帶有指定標籤名的對象集合。open()//開啟一個流,以收集來自任何 document.write() 或 document.writeln() 方法的輸出。write()//向文檔寫 HTML 運算式 或 JavaScript 代碼。writeln()//等同於 write() 方法,不同的是在每個運算式之後寫一個分行符號。 舉例:input標籤聚焦和失焦來觸發方法<input class="rg" name="username" type="text" placeholder="請設定使用者名稱"  onFocus="showMsg()"            onBlur="hiddenMsg()" >        <div id="Msg" style="width:120px; float:right; display: none;"  ><span> 設定後不可更改</span></div><script>//調用script,可以通過js來修改CSS樣式function showMsg(){document.getElementById("Msg").style.display="block";}function hiddenMsg(){document.getElementById("Msg").style.display="none";}      </script>f.jQueryjQuery 是一個 JavaScript 庫。jQuery 極大地簡化了 JavaScript 編程。jQuery 很容易學習。 舉例:用jQuery來寫一個隱藏的JS語句<script src="/jquery/jquery-1.11.1.min.js">//先調用jquery的js庫,類似於java中的包</script><script>$(document).ready(function(){   $("p").click(function(){  //p標籤的點擊事件$(this).hide();      //p標籤隱藏  });});</script>   

三、JS基礎

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.