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

查看:50
本文介绍了使用 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天全站免登陆