以下代码有效:
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;
}
Run Code Online (Sandbox Code Playgroud)
我想获得图像的原始宽度和高度,并尝试使用不同的代码来实现它.我的功能触发点击事件,所有图像都调整为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');
}
Run Code Online (Sandbox Code Playgroud)
附加auto没有改变图像的大小.
alert(id.attr('width'));总是返回'undefined'.所以它不起作用
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;
}
Run Code Online (Sandbox Code Playgroud)
alert(image.width); 工作得很好.
alert(image.heigth); …