jquery image width()在IE9中未返回正确的值
我们有一个图像标签,其宽度和高度指定为“自动”,以便我们可以获得实际图像的宽度和高度。因此,我们使用以下方法来获取它们jquery image width()在IE9中未返回正确的值,jquery,Jquery,我们有一个图像标签,其宽度和高度指定为“自动”,以便我们可以获得实际图像的宽度和高度。因此,我们使用以下方法来获取它们 $("img.list-image").width() $("img.list-image").height() 在FF、Chrome、IE8和safari中,返回的宽度和高度分别为125像素和160像素,这些值是正确的。但在IE9中,宽度为1519像素,高度为771像素。由于某些原因,这些宽度和高度函数在IE9中没有返回正确的值。感谢您的帮助 编辑: 下面是我们正在使用的代
$("img.list-image").width()
$("img.list-image").height()
在FF、Chrome、IE8和safari中,返回的宽度和高度分别为125像素和160像素,这些值是正确的。但在IE9中,宽度为1519像素,高度为771像素。由于某些原因,这些宽度和高度函数在IE9中没有返回正确的值。感谢您的帮助
编辑:
下面是我们正在使用的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Image Page</title>
<style type="text/css" >
.list-category-image
{
border-width: 0px;display:none;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
//<![CDATA[
$(function() {
categoryImageaspectratio();
});
function categoryImageaspectratio() {
$("img.list-category-image").one('load', function() {
var width = 85.0, height = 85.0;
var realWidth, realHeight;
realWidth = $(this).width(); // Note: $(this).width() will not
realHeight = $(this).height();
//alert("width, height = (" + realWidth + "," + realHeight + ")");
// Now scale the image.
var aspectRatio = 1.0;
if (realHeight / height > realWidth / width) {
aspectRatio = realHeight / height;
width = realWidth / aspectRatio;
}
else {
aspectRatio = realWidth / width;
height = realHeight / aspectRatio;
//alert("aspectRatio = " + aspectRatio);
//alert("height = " + height);
}
var imgWidth = parseInt(width) + "px";
var imgHeight = parseInt(height) + "px";
//alert(imgWidth + ", " + imgHeight);
// $(this).css("width", imgWidth).css("heigth", imgHeight).css('display', 'block');
$(this).css("width", imgWidth).css("heigth", imgHeight).css('display', 'block').css('text-align', 'center').css('margin-left', 'auto').css('margin-right', 'auto');
}).each(function() {
if (this.complete) {
$(this).load();
}
});
}
</script>
</head>
<body>
<div style="width:85px; height:85px;">
<img class="list-category-image" src="http://ecx.images-amazon.com/images/I/411j0fCdVKL._SL160_.jpg" alt="" />
</div>
</body>
</html>
图像页
.列表类别图像
{
边框宽度:0px;显示:无;
}
//真实宽度/宽度){
aspectRatio=真实高度/高度;
宽度=realWidth/aspectRatio;
}
否则{
aspectRatio=真实宽度/宽度;
高度=真实高度/纵横比;
//警报(“aspectRatio=“+aspectRatio”);
//警报(“高度=”+高度);
}
var imgWidth=parseInt(宽度)+“px”;
var imgHeight=parseInt(高度)+“px”;
//警报(imgWidth+“,”+imgHeight);
//$(this.css(“width”,imgWidth).css(“heigth”,imgHeight).css(“display”,“block”);
$(this).css(“width”,imgWidth).css(“heigth”,imgHeight).css('display','block')).css('text-align','center').css('margin-left','auto').css('margin-right','auto');
}).each(函数({
如果(完成此项){
$(this.load();
}
});
}
使用此关键字总是非常棘手,有时会令人困惑,在不同的javascript实现中可能会有不同的值。尝试将图像定义为变量,以查看它是否解决了您的问题:
var imgList = $("img.list-category-image");
imgList.one('load', function() {
var width = 85.0, height = 85.0;
var realWidth, realHeight;
realWidth = imgList.width(); // Note: $(this).width() will not
realHeight = imgList.height();
您可以使用以下代码检查此类型:
alert(typeof(this));
使用这个关键字总是非常棘手,有时会令人困惑,在不同的javascript实现中可能会有不同的值。尝试将图像定义为变量,以查看它是否解决了您的问题:
var imgList = $("img.list-category-image");
imgList.one('load', function() {
var width = 85.0, height = 85.0;
var realWidth, realHeight;
realWidth = imgList.width(); // Note: $(this).width() will not
realHeight = imgList.height();
您可以使用以下代码检查此类型:
alert(typeof(this));
你能给我们提供一个(非)工作的演示吗?你使用的是最新的jQuery稳定版本吗?旧版本可能不支持IE9。我添加了代码来复制issue@mohang上述三人中没有人看到您的评论或编辑-您需要使用
@
通知他们发送通知。在本测试页面中使用IE9获取125160
:您能为我们提供一个(非-)正在运行的演示?您使用的是最新稳定版本的jQuery吗?旧版本可能不支持IE9。我添加了代码来复制issue@mohang上述三个人中没有人看到您的评论或编辑-您需要使用@
通知每个人向他们发送通知。在这个测试页面中使用IE9获取125160
:但是我们有几个需要的图像,因此,可能需要一个循环来实现这一点。但是,我们通过添加jQuery1.5.1版本解决了这个问题。谢谢你的努力。和+1。但是我们有几个需要的图像,所以可能需要一个循环来实现这一点。但是,我们通过添加jQuery1.5.1版本解决了这个问题。谢谢你的努力。和+1表示感谢您的努力。