此段代码用来重置文章内容中宽度超出范围的图片

<script>
//此段代码用来重置文章内容中宽度超出范围的图片
function changeImgSize() {
 
var Article_Content_ID = "Label4"//文章内容所在的Div的ID
 var MAX_Width = 700//最大图片宽度
 
 
var imgs = document.getElementById(Article_Content_ID).getElementsByTagName("img");
 
for ( var i = 0,len = imgs.length;i<len;i++ ){
  
if( parseInt(imgs[i].width,10> MAX_Width ){
   
//alert(i+":宽度进行调整");
   var iw = parseInt(imgs[i].width,10) ;
   
var ih = parseInt(imgs[i].height,10) ;
   imgs[i].style.width 
= MAX_Width +"px";
   imgs[i].style.height 
=  parseInt( ih * MAX_Width / iw ) + "px";
  }
  imgs[i].border
='0';//由于添加连接后有边框,再此处设为0
  var outerHTML_temp = imgs[i].outerHTML; //获取img对象内容
  outerHTML_temp="<a href='"+imgs[i].src+"' target='_blank'>"+outerHTML_temp+"</a>"//拼接href
  //alert(outerHTML_temp);
  imgs[i].outerHTML = outerHTML_temp; //输出新的标签
 }
}
//兼容FireFox
if (window.HTMLElement) {
   HTMLElement.prototype.__defineSetter__(
"outerHTML",function(sHTML) {
         
var r=this.ownerDocument.createRange();
         r.setStartBefore(
this);
         
var df=r.createContextualFragment(sHTML);
         
this.parentNode.replaceChild(df,this);
         
return sHTML;
     });

     HTMLElement.prototype.__defineGetter__(
"outerHTML",function() {
     
var attr;
         
var attrs=this.attributes;
         
var str="<"+this.tagName.toLowerCase();
         
for (var i=0;i<attrs.length;i++) {
             attr
=attrs[i];
             
if(attr.specified)
                 str
+=" "+attr.name+'="'+attr.value+'"';
         }
         
if(!this.canHaveChildren)
             
return str+">";
         
return str+">"+this.innerHTML+"</"+this.tagName.toLowerCase()+">";
         });

   HTMLElement.prototype.__defineGetter__(
"canHaveChildren",function() {
     
switch(this.tagName.toLowerCase()) {
         
case "area":
         
case "base":
         
case "basefont":
         
case "col":
         
case "frame":
         
case "hr":
         
case "img":
         
case "br":
         
case "input":
         
case "isindex":
         
case "link":
         
case "meta":
         
case "param":
         
return false;
     }
     
return true;
   });
}

changeImgSize();
</script>
原文地址:https://www.cnblogs.com/starlet/p/1384592.html