分享java帶來的快樂

          我喜歡java新東西

          js控制圖片的顯示最寬和最高值,取js對象的全部屬性

          <html>
          <head>
          <script>


           

          function allProps(obj){
              // 用來保存所有的屬性名稱和值
             var props = "";
           
              // 開始遍歷
             if(typeof obj== "string"){
              props =obj;
             }else{
              if(obj!=null&& (typeof obj =="object")){
                for(var p in obj){
                    // 方法
                    if(typeof(obj[p])=="function"){ 
                      
                    }else{ 
                        // p 為屬性名稱,obj[p]為對應屬性的值
                        props+= p + "=" + obj[p] + "\r\n";
                    }
                }
              }
             }


              // 最后顯示所有的屬性
              alert(props);
          }

          //js控制圖片的顯示最寬和最高值
          function drawImage(ImgId,maxwidth,maxheight){
           //本程序將圖像控制在寬為maxwidth且高為maxheight的框內
           //ImgD是圖像ID,maxwidth、maxheight是圖像最大顯示寬度和高度
           var ImgD=document.getElementById(ImgId);
           allProps(ImgD);
           var image=new Image();
           image.src=ImgD.src;
           var imgwidth=image.width;
           var imgheight=image.height;
           if(imgwidth>0 && imgheight>0 && maxwidth>0 && maxheight>0){
            if(imgwidth>maxwidth || imgheight>maxheight){
               if(imgwidth/imgheight>= maxwidth/maxheight){
                 ImgD.width=maxwidth;
                 ImgD.height=(imgheight*maxwidth)/imgwidth;
               }
               else{
                ImgD.height=maxheight;
                ImgD.width=(imgwidth*maxheight)/imgheight;
               }
            }else{
               ImgD.width=imgwidth;
               ImgD.height=imgheight;
            }
           }
          }
          </script>
          </head>
          <body>
           <img id="thisimage" src="http://d5.sina.com.cn/201101/31/283911_750-450.jpg">
           <script>
            drawImage('thisimage',80,80);
           </script>
          </body>
          </html>

          posted on 2010-02-09 13:24 強強 閱讀(827) 評論(0)  編輯  收藏 所屬分類: web技巧

          主站蜘蛛池模板: 莒南县| 长乐市| 温州市| 榆树市| 鱼台县| 商南县| 昌吉市| 广东省| 黑山县| 安陆市| 尖扎县| 达日县| 民勤县| 绵竹市| 西充县| 甘肃省| 天镇县| 葫芦岛市| 武宁县| 勃利县| 曲沃县| 湟源县| 浠水县| 凤山市| 华蓥市| 新干县| 札达县| 丰城市| 玉龙| 宁陵县| 承德市| 临武县| 泊头市| 铁岭市| 富裕县| 安塞县| 崇阳县| 金沙县| 错那县| 平昌县| 龙口市|