分享java帶來的快樂

          我喜歡java新東西

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

          <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]為對(duì)應(yīng)屬性的值
                        props+= p + "=" + obj[p] + "\r\n";
                    }
                }
              }
             }


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

          //js控制圖片的顯示最寬和最高值
          function drawImage(ImgId,maxwidth,maxheight){
           //本程序?qū)D像控制在寬為maxwidth且高為maxheight的框內(nèi)
           //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 強(qiáng)強(qiáng) 閱讀(826) 評(píng)論(0)  編輯  收藏 所屬分類: web技巧

          主站蜘蛛池模板: 万山特区| 舟山市| 城市| 宁晋县| 康马县| 丹阳市| 九龙县| 桐庐县| 雅安市| 门源| 棋牌| 平顺县| 南宫市| 高平市| 永济市| 呼图壁县| 行唐县| 尖扎县| 青神县| 彰武县| 金昌市| 祥云县| 五河县| 镇巴县| 太和县| 马边| 高邮市| 乌兰县| 习水县| 教育| 上饶市| 崇阳县| 富平县| 多伦县| 江山市| 宁远县| 金堂县| 昌都县| 永善县| 九寨沟县| 镇原县|