使用JavaScript更改图像大小 [英] Change image size with JavaScript

查看:194
本文介绍了使用JavaScript更改图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript更改图片的大小。 jS文件与HTML页面分开。

I'm trying to change the size of an image with JavaScript. The jS file is separate from the HTML page.

我想在JS文件中设置图像的高度和宽度。这样做的好方法是什么?

I want to set the height and width of an image in the JS file. Any good ways on doing this?

推荐答案

一旦你有了对图像的引用,就可以像这样设置它的高度和宽度:

Once you have a reference to your image, you can set its height and width like so:

var yourImg = document.getElementById('yourImgId');
if(yourImg && yourImg.style) {
    yourImg.style.height = '100px';
    yourImg.style.width = '200px';
}

在html中,它看起来像这样:

In the html, it would look like this:

<img src="src/to/your/img.jpg" id="yourImgId" alt="alt tags are key!"/>

这篇关于使用JavaScript更改图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆