jQuery获取高度和宽度
问题描述:
我做了if
函数来检查宽度是否为< 100px。由于某种原因,它隐藏了一切。有人知道为什么jQuery获取高度和宽度
$(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");
}
});
});
答
试试这个:
$(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);
});
});
答
您使用pic
时,你应该使用$(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);
});
你也应该留意是否被调整与CSS的图像。取而代之的
$this.removeAttr("width");
$this.removeAttr("height");
尝试
$this.css({width: 'auto', height: 'auto'});
答
我想尽解决方案,在这里,但没有为我工作,所以我创建了自己的脚本,它的工作原理包括IE在内的所有浏览器。下面我的脚本:
$(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;
}
我希望这也能与你一起工作!
答
$this.css({width: 'auto', height: 'auto'});
尝试
它的工作,谢谢。 var pic_real_width = $ this.width(); var pic_real_height = $ this.height();如果(pic_real_width Carvefx 2009-12-09 12:13:27
请参阅我对您最新问题的回答:http://stackoverflow.com/questions/1873536/jquery-height-condition/1873546#1873546 我已经在那里回答您。 – 2009-12-09 12:19:31