如何使用 jquery 设置图像的高度、宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2183863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:57:22  来源:igfitidea点击:

How to set height , width to image using jquery

jquery

提问by Vicky

Is there any way to set height and width of an image using jquery? The following is my code

有没有办法使用jquery设置图像的高度和宽度?以下是我的代码

var img = new Image();  
 // Create image
$(img).load(function(){                 
    imgdiv.append(this);
}).error(function () {  
    $('#adsloder').remove();
}).attr({ 
    id: val.ADV_ID,  
    src: val.ADV_SRC,
    title: val.ADV_TITLE,
    alt: val.ADV_ALT
});

Thanks.

谢谢。

回答by Nick Craver

You can call the .height()and ,.width()setters:

您可以调用.height()和 , .width()设置器:

var img = new Image();  
 // Create image
$(img).load(function(){                 
    imgdiv.append(this);
}).error(function () {  
    $('#adsloder').remove();
}).attr({ 
    id: val.ADV_ID,  
    src: val.ADV_SRC,
    title: val.ADV_TITLE,
    alt: val.ADV_ALT
}).height(100).width(100);

回答by sekhar.k

$(".img1").css('height','10');
$(".img1").css('width','10');

OR

或者

$(".img1").attr('height','10');
$(".img1").attr('width','10');

回答by Chetabahana

In case the image is inside <div class="myGallery">

如果图像在里面 <div class="myGallery">

$('div.myGallery > img').css({ 'height': '10px', 'width': '10px' });