使用变量在javaScript中设置样式 [英] Use variables to set in style with javaScript

查看:90
本文介绍了使用变量在javaScript中设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function Scale(){
    X = innerWidth / 1024;
    Y = innerHeight / 768;

    Z = document.getElementById("IFM");
    Z.style.transform = "scale(X, Y)";
}

我对此代码有问题。我无法在规模中使用变量!

我可以用这个问题做什么?

I have a problem with this code. I couldn't use variables in scale!
What can I do with this problem?

推荐答案

JavaScript没有内联字符串变量。您可以做的是连接字符串:

JavaScript has no inline string variables. All you can do is concatenate strings:

Z.style.transform = "scale("+X+", "+Y+")";

这些数字将隐式转换为字符串。

The numbers will be implicitly converted to strings.

您也可以使用某些自定义 sprintf -like 格式化或替换方法,但通常与 + 的连接更简单。

You might as well use some custom sprintf-like format- or replace-methods, but usually concatenation with + is simpler.

这篇关于使用变量在javaScript中设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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