开发者

jQuery get height & width

I made an if function to check if the width is < 100px. For some reason it hides everything. Anyone know why?

$(document).ready(function() {
var pic = $(".pic");

// need to remove these in of case img-element has set width and height
$(".pic").each(function() {
    var $this 开发者_开发百科= $(this);
    $this.removeAttr("width"); 
    $this.removeAttr("height");

    var pic_real_width = $this.width();
    var pic_real_height = $this.height();
    if(pic_real_width<100){
    $(this).css("display","none");
    }
    });

 });


You're using pic when you should be using $(this):

$(".pic").each(function() {
    var $this = $(this);
    $this.removeAttr("width"); 
    $this.removeAttr("height");

    var pic_real_width = $this.width();
    var pic_real_height = $this.height();
    alert(pic_real_width);
     });

You should also watch for images that are resized with CSS.

Instead of

    $this.removeAttr("width"); 
    $this.removeAttr("height");

try

$this.css({width: 'auto', height: 'auto'});


Try this:

$(document).ready(function() {
    $(".pic").each(function() {
        var pic = $( this );
        pic.removeAttr("width"); 
        pic.removeAttr("height");

        var pic_real_width = pic.width();
        var pic_real_height = pic.height();
        alert(pic_real_width);
    });
});


I tried every solutions here but none worked for me so I created my own script and it works in all browsers including IE. Heres my script:

$(function(){
 var sbar = $(".sidebar").outerHeight(true);
 var pcont = $(".post_content").outerHeight(true);
if(sbar < pcont){
  $(".sidebar").css("min-height",pcont+"px");
}
});

/*** CSS ***/

.sidebar{
   float:left;
   min-height: 500px;
   width:180px;
}

.post_content{
   float:left;
   min-height: 500px;
   width:593px;
}

I hope this will work with you too!!


$this.css({width: 'auto', height: 'auto'});

try it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜