小编Cad*_*noX的帖子

如何获得图像的原始大小-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;
}
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); …

jquery height width

7
推荐指数
1
解决办法
3088
查看次数

标签 统计

height ×1

jquery ×1

width ×1