js重置圖片的高寬

字號:


    <script type=text/javascript>
    //正文圖片內(nèi)容溢出處理
    function redrawimage(obj) {
    var boxwidth = 680; // $(obj).width() - 20;
    $(obj + img).each(function () {
    var imgwidth = $(this).width();
    var imgheight = $(this).height();
    if (imgwidth > boxwidth) {
    $(this).width(boxwidth);
    $(this).height(boxwidth / imgwidth * imgheight);
    }
    })
    $(obj + table table).each(function () {
    if ($(this).width() > boxwidth) {
    $(this).width(boxwidth);
    $(this).height(boxwidth / $(this).width() * $(this).height());
    }
    })
    $(obj + tr tr).each(function () {
    var trimg = $(this).find('img');
    if (trimg.length > 1) {
    var trimgwidth = (boxwidth / trimg.length);
    trimg.each(function () {
    if ($(this).width() > trimgwidth) {
    $(this).width(trimgwidth);
    $(this).height(trimgwidth / $(this).width() * $(this).height());
    }
    });
    }
    })
    }
    $(window).load(function () {
    redrawimage(.newpic);//可以是id,或者類 (要設(shè)置的范圍)
    });
    </script>