css 限制图片最大尺寸 要兼容IE6-IE8的

2025-02-24 21:51:00
推荐回答(2个)
回答1:

javascript限制代码为:中加入onload="javascript:if(this.width>530)this.width=530" 530宽度css限制为:css里面加入.change img {
max-width: 530px; width:e­xpression(this.width > 530 ? "530px" : this.width)
}图片的class中加入change就好 例如class="change"或者class="xxx change"其中xxx是原图片样式! 谢谢采纳!

回答2:

function attachImgEventListener()
{
var imageArray=document.getElementById("imgwidth").getElementsByTagName("img");
for(var i=0;i {
if(imageArray[i].offsetWidth>500)//当图片的宽度超过500
{
imageArray[i].style.width="500px";//就让他等于500 否则按原图显示
}

}
}
function addLoadEvent(func){
var oldonload = window.onload;
if (typeof window.onload != "function"){
window.onload = func;
}else{
window.onload = function(){
oldonload();
func();
}
}
}
addLoadEvent(attachImgEventListener); //imgwidth是装图片的容器 id 不知道是否是你需要的 这是js代码哦