JavaScript對象應用-字串和圖片對象

來源:互聯網
上載者:User

標籤:

1.1 應用 String對象截取特定文字

  利用String 對象的charAt() 和 substring() 方法等,截取特定文字或欄位文字顯示在頁面上

 1 <html> 2  <head> 3   <meta http-equiv="Content-Type" content="text/html"; charset="gb2312"> 4   <title>字串對象應用執行個體</title> 5   <script language="JavaScript"> 6        <!--  7         function CheckSpace(string) 8         { 9             var  index,len10              while(true)11              {12                  index=string.indexOf(" ");13                  //如果沒有空格,就終止14                   if(index==-1)15                   break;16                   //求出字串的長度17                   len=string.length;18                   //刪除空格19                   string=string.substring(0,index)+string.substring((index+1),len);20 21                }22             return string23 24          }25         -->26  </script>27 </head> 28 <body>29     <script language="JavaScript">30           <!--31               var s1=new String("I love China !");32               document.write("<h1>[1]"+s1.charAt(7)+"<br>");33 34               var s2=new String("中國首都是北京");35               document.write("[2]"+s2.charAt(4)+"<br>");36              37               var s3=new String("I love China !");38               document.write("[3]"+s3.substring(1,6)+"<br>");39               40               document.write("[4]"+"中國的首都是北京".substring(0,5)+"<br>");41 42               document.write("[5]"+"I love China !".substring(4)+"<br>");43 44               document.write("[6]"+"I love China !".substring(5,2)+"<br>");45               document.write("<h1>");46 47               var str=""48               str+=CheckSpace("I am a student.")+"\n";49               str+=CheckSpace("成了一名 奧運 志願者!");50               alert(str);51            -->52       </script>53 </body>54 </html>

 1.2 應用 Image 對象實現動畫

 1 <html> 2    <head> 3    <title>應用image對象實現動畫</title> 4    </head> 5    <script  language="JavaScpript"> 6      var  myImage=new Array[10]; 7       for(i=0;i<11; i++) 8              myImage[i] =new Image(); 9       myImage[0].src="pic/t0.jpg";10       myImage[1].src="pic/t1.jpg";11       myImage[2].src="pic/t2.jpg";12       myImage[3].src="pic/t3.jpg";13       myImage[4].src="pic/t4.jpg";14       myImage[5].src="pic/t5.jpg";15       myImage[6].src="pic/t6.jpg";16       myImage[7].src="pic/t7.jpg";17       myImage[8].src="pic/t8.jpg";18       myImage[9].src="pic/t9.jpg";19       myImage[10].src="pic/t10.jpg";20       21       var k=0;22            function  changePIC(){23            document.mi1.src=[k].src;24            k++;25            if(k==9)26               k=0;27            setTimeout(changePIC,200);28       }29   </script>30   <body>31      <img name="mi1"  src="pic/t0.jpg">32       <script  language="JavaScript">33             changPIC();34        </script>35   </body>36 </html>

1.3 Style 對象應用

 1 <html> 2  <head> 3       <title>style 對象應用</title> 4 </head> 5 <script  language="JavaScript"> 6      function  changeTablePro(){ 7          MTB.style.borderLeft="solid  red 2px"; 8          MTB.style.borderRight="solid  red 2px"; 9          MTD1.style.borderLeft="solid  blue 2px";10          MTD1.style.borderRight="solid  blue 2px";11          MTD2.style.borderLeft="solid  blue 2px";12          MTD2.style.borderRight="solid  blue 2px";13          MTD3.style.borderLeft="solid  blue 2px";14          MTD3.style.borderRight="solid  blue 2px";15          MTD4.style.borderLeft="solid  blue 2px";16          MTD4.style.borderRight="solid  blue 2px";17    }18     function resetTablePro(){19          MTB.style.borderLeft="solid  blue 1px";20          MTB.style.borderRight="solid  blue 1px";21          MTD1.style.borderLeft="solid  red 1px";22          MTD1.style.borderRight="solid  red 1px";23          MTD2.style.borderLeft="solid  red 1px";24          MTD2.style.borderRight="solid  red 1px";25          MTD3.style.borderLeft="solid  red 1px";26          MTD3.style.borderRight="solid  red 1px";27          MTD4.style.borderLeft="solid  red 1px";28          MTD4.style.borderRight="solid  red 1px";29     }30 <script>31 <body id="BD"  style="background:url(back49.gif)repeat fixed;">32   <p>33   <br>34   <pre>35    這裡是關於style 對象的應用執行個體,我們將通過對 style 對象的應用來改變頁面的背景屬性36   <br>37   </pre>38   <from>39       <input type="button" value="背景映像滾動" onclick="JavaScript:BD.style.backgroundAttachment=‘scroll‘ ">40       <input type="button" value="背景映像靜止"  onclick="JavaScript:BD.style.backgroundAttachment=‘fixed‘">41 <p>42        <input type="button" value="清除背景映像" onclick="JavaScript:BD.style.backgroundImage=‘ ‘ ">43         <input type="button" value="添加背景映像" onclick="JavaScript:BD.style.backgroundImage=‘url(back49.gif)‘">44 <p>45         <input  type="button" value="還 原 背 景 初 始 設 置"  onclick="JavaScript:BD.style.background=‘url(back49.gif)repeat fixed‘">46 <p>47         <input type="button  value="改變表格邊框屬性""  onclick="changeTablePro()">48 <p>49         <input type="button  value="還原表格初始設定" onclick="resetTabelPro()">50  </from>51 <p>52  <table id="MTB" style="border-left:solid blue 1px;border-right:dotted blue 1px;">53   <tr> 54       <td id="MTD1" style="border-left:solid red 1px;border-right:solid 1px;padding:10px;spacing:10px;">55        <pre>56          這個執行個體中,初始化設定了網頁背景映像,在水平和垂直方向重複顯示並靜止不動,不隨捲軸的拖動而滾動。57        </pre>58       </td>59       <td id="MTD2" style="border-left:solid red 1px;border-right:solid 1px;padding:10px;spacing:10px;">60       <pre>61         當利用按一下滑鼠各個按鈕是,會見到所發生的改變。62       </pre>63       </td>64   </tr>65   <tr>66        <td id="MTD3" style="border-left:solid red 1px;border-right:solid red 1px;padding:10px;spacing:10px;">67      <pre>68        每一種改變和變化,都是針對特定的id 才隨著變化的。69      </pre>70      </td>71       <td id="MTD4" style="border-left:solid red 1px;border-right:solid red 1px;padding:10px;spacing:10px;">72      <pre>      73        本執行個體中的六個按鈕,執行相應程式後會對頁面背景屬性,表格屬性變更。 74       程式碼中都採用 style 對象屬性來調用相應的屬性,進行控制實現各種變化。75      </pre>76      </td>77   </tr>78 </table>79 </body>80 </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.