如何定义图像的大小,如宽度和高度,这是从服务器加载

how to define the size of image like width and height which is loaded from the server

本文关键字:高度 加载 服务器 何定义 定义 图像      更新时间:2023-09-26

我已经成功地从服务器加载图像并将其显示到div元素,但我想调整图像的大小,因为它是大的。我怎么能做到呢?这是我从服务器加载图像的代码。

var img = $("<img />").attr('src', url)
    .load(function() {
        if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
            alert('broken image!');
        } else {
            $("#image").append(img);
        }
    });

可以使用width()height()的getter和setter

var xDimension=150;
$(img).load(function(){ 
     // put your logic here
  }).width(xDimension).height(xDimension);

在css文件中添加如下样式:

#image img{width: your width; height: your height;}