7
解决:语法错误
下面的代码工作:如何获得图像的原始尺寸-Jquery
var image = {width:0,height:0};
var imageObject = new Image();
function getImageSize(id) {
imageObject.src = id.attr("src");
image.width = imageObject.width;
image.height = imageObject.height;
}
原始的问题
我想获得图像的原始宽度和高度,并尝试使用不同的代码来实现它。我的功能触发点击事件和所有图像的大小调整为400x300。
我试图将图像尺寸调整到原始大小使用auto
:
var image = {width:0,heigth:0};
function getImageSize(id) {
id.css({
'width': 'auto',
'heigth': 'auto'
});
image.width = id.attr('width');
image.heigth = id.attr('heigth');
}
附加auto
没有改变图像的大小。
alert(id.attr('width'));
总是返回'未定义'。所以它不起作用
var image = {width:0,heigth:0};
var imageObject = new Image();
function getImageSize(id) {
imageObject.src = id.attr("src");
image.width = imageObject.width;
image.heigth = imageObject.heigth;
}
alert(image.width);
完美的作品。
alert(image.heigth);
返回未定义。
使用imageObject.naturalWidth
的工作方式如上 - 宽度给定正确,但heigth未定义。
我读过,你必须追加新的图像对象,但是当我使用imageObject.appendTo('body');
我甚至不能点击图像。
感谢您的任何建议。
一旦你改变图像大小,自动将不会再次把它。至于获取图像的大小,它必须先加载。 – adeneo
'image.heigth'语法错误:“高度” –
哦,我写错了很多次, 更改语法解决了它 – CadanoX