HTML常用資料類型以及例題練習

來源:互聯網
上載者:User

標籤:

常用的類型:
1.數學:
Math.ceil():天花板數
Math.floor():地板數
Math.round():四捨五入取整數
Math.random():產生0-1之間的隨機數

2.日期時間:
var s = new Date();
var s = new Date(1999,7,23);
函數:
getFullYear():
getMonth():
getDate():擷取日
getHours()
getMinutes()
getSeconds()
getDay():擷取星期
setFullYear(year,month,day)
setHours(hour,minute,second,ms)
toLocaleDateString():轉化為本地的日期時間格式字串

3.字串
length:字串的長度
toLowerCase()
toUpperCase()

//壓縮字串中的兩端空格。
function   Trim(m){  
  while((m.length>0)&&(m.charAt(0)==‘ ‘))  
  m   =   m.substring(1, m.length);  
  while((m.length>0)&&(m.charAt(m.length-1)==‘ ‘))  
  m = m.substring(0, m.length-1);  
  return m;  
}

indexOf("子串"):第一次出現的位置
lastIndexOf("子串"):最後一次出現的位置
以上兩個方法,如果父串中不包含子串,則返回-1

substr("起始位置","截取長度")
substring("起始位置","結束位置")

 

事件:(事件來源,事件數目據,事件處理常式)
一、最常用的滑鼠事件
onclick:當按一下滑鼠的時候,要執行的代碼。
ondblclick:當滑鼠雙擊的時候,要執行的代碼。
onmouseover:當滑鼠移動到元素上時,要執行的代碼。
onmouseout:當滑鼠從元素上離開時,要執行的代碼。
<樣本>光棒效果。
onmousedown:
onmouseup:
<樣本>使用這兩個事件,用span做出一個類比的按鈕。

案例:刮刮樂

二、鍵盤:


三、焦點:

 

 

例題:表格隨滑鼠移動改變顏色

<style type="text/css">.aa{    background-color:#0F3;    }.bb{    background-color:#990;}</style></head><body><table style="width:500px;height:100px;">  <tr class="aa" onmouseover="doOn(this)" onmouseout="doOver(this)">    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr>  <tr class="bb" onmouseover="doOn(this)" onmouseout="doOver(this)">    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr>  <tr class="aa" onmouseover="doOn(this)" onmouseout="doOver(this)">    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr>  <tr class="bb" onmouseover="doOn(this)" onmouseout="doOver(this)">    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr>  <tr class="aa" onmouseover="doOn(this)" onmouseout="doOver(this)">    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr></table></body></html><script language="javascript">function doOn(dd){        //bg不能加var    bg=dd.style.backgroundColor;    dd.style.backgroundColor="yellow";}function doOver(dd){    dd.style.backgroundColor=bg;    }</script>

 

例題:按鈕的顏色、樣式改變

<!--當按下按鈕,按鈕顏色改變,位置位移,抬起時,按鈕恢複原樣--><style type="text/css">span{    /*要想span元素顯示寬度,必須設定display:inline-block*/    display:inline-block;    width:80px;    height:30px;    background-color:#CF3;    border:2px black solid;    text-align:center;    line-height:30px;    vertical-align:middle;    }</style></head><body><span onmousedown="doDown(this)" onmouseup="doUp(this)">按鈕</span></body></html><script language="javascript">function doDown(dd){    dd.style.backgroundColor="blue";    dd.style.border="3px gray solid"        dd.style.margin="3px 0px 0px 3px";        }function doUp(aa){    aa.style.backgroundColor="#CF3";    aa.style.border="3px black solid";    aa.style.margin="0px 0px 0px 0px";        }</script>

例題:刮刮樂

<style type="text/css">.pic{    width:128px;    height:128px;    border:3px solid blue;    float:left;    margin:5px 5px;    position:relative;    left:300px;}    /*遮擋圖片*/.she{    width:128px;    height:128px;    background-color:#FF9;    }    </style><script language="javascript">function doOpen(dd){     dd.style.display="none";                var s=dd.parentNode.getAttribute("haha");    if (s=="1")        {        alert("恭喜中大獎");        window.location.reload();        }                //圖片翻完,自動重新整理    num+=1;    if(num==10)    {alert("歡迎再次光臨");        window.location.reload();        }            }</script></head><body style="width:750px;"><div class="pic"><div class="she" onclick="doOpen(this)"></div></div><div class="pic"><div  class="she" onclick="doOpen(this)"></div></div><div class="pic"><div  class="she" onclick="doOpen(this)"></div></div><div class="pic"><div  class="she"  onclick="doOpen(this)"></div></div><div class="pic"><div  class="she" onclick="doOpen(this)"></div></div><div class="pic"><div  class="she" onclick="doOpen(this)"></div></div><div class="pic"><div  class="she" onclick="doOpen(this)"></div></div><div class="pic"><div class="she" onclick="doOpen(this)"></div></div><div class="pic"><div  class="she" onclick="doOpen(this)"></div></div><div class="pic"><div  class="she" onclick="doOpen(this)"></div></div></body></html><script language="javascript">var num=0;//對元素隨機載入圖片var a=document.getElementsByTagName("div");for(var i=0;i<a.length;i++){    if(a[i].className=="pic")    {        var n=(Math.round(Math.random()*1000000))%10+1;//注意先取整數,再取餘;    var path="images/"+n+".png";    a[i].style.backgroundImage="url("+path+")";    //設定大獎        if(n == 1)        {            //設定一個屬性,標記該元素            a[i].setAttribute("haha","1");        }    }}</script>

 

例題:手機號抽獎

<style type="text/css">#phone{    width:200px;    height:30px;    font-size:24px;}    </style></head><body><span id="phone">130000000000</span><div id="btn" onclick="doDis()">開始</div></body></html><script language="javascript">var a=new Array();a[0]="13512353421";a[1]="13532157534";a[2]="15932547667";a[3]="15834223332";a[4]="15467954466";a[5]="15454234567";a[6]="18345678877";a[7]="18344332165";a[8]="18732573457";a[9]="18346575243";var pho=document.getElementById("phone");var btn=document.getElementById("btn");function doRec(){    var n=Math.round(Math.random()*100000)%10;        pho.innerHTML=a[n];        aaa=window.setTimeout("doRec()",50);    }function doDis(){    if(btn.innerHTML=="開始")    {                btn.innerHTML="停止";        doRec();    }    else    {                btn.innerHTML="開始";        window.clearTimeout(aaa);}        }</script>

HTML常用資料類型以及例題練習

聯繫我們

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