[英]I need variables to pass between a .js file, an .html and a .css file. How do I do this?
I have three files inside of my script. 我的脚本中有三个文件。 The purpose of the script is to take the battery from the device (Which is working) display it on my background (which is also working) and then change the color depending on the level of battery (Which is not working).
该脚本的目的是从设备中取出电池(正在工作)在我的背景上显示电池(也在工作),然后根据电池电量(不工作)更改颜色。
These are the three files in question: 这些是有问题的三个文件:
Battery.js Battery.js
Background.html Background.html
and style.css 和style.css
In my Battery.js I have a calculation method that calculates the amount of battery left in my phone. 在Battery.js中,我有一种计算方法,可以计算手机中剩余的电量。 Defined by: "level".
定义者:“级别”。
if (level>=50) {
rb=0;
gb=240;
bb=240;
}
if (level <50) {
rb=100;
gb=140;
bb=140;
}
if (level <20) {
rb=230;
gb=30;
bb=30;
}
Basically I have three variables (rb, gb, and bb) that are each a color code for an "rgb(r,g,b)" code. 基本上,我有三个变量(rb,gb和bb),它们都是“ rgb(r,g,b)”代码的颜色代码。 In which I would type out as "rgb(rb,gb,bb)".
我将在其中键入“ rgb(rb,gb,bb)”。
Inside this same battery.js file I have this code that defines how the battery is calculated and shown on the screen: 在同一个battery.js文件中,我有以下代码,该代码定义了电池的计算方式并显示在屏幕上:
$("#battery").html(level+'%');
In my style.css file I have: 在我的style.css文件中,我有:
#battery{
font-size: 25px;
Inside of Background.html I have two blocks of code: 在Background.html内部,我有两个代码块:
<script type="text/javascript" charset="utf-8" src="Scripts/battery.js"></script>
And: 和:
<p id="battery"></p>
If I place 如果我放
color: rgb(rb,gb,bb);
in style.css in the appropriate place, nothing happens. 在style.css中的适当位置,什么也没有发生。
If I place that same code inside of background.html like this: 如果我将相同的代码放在background.html中,如下所示:
<font color="rgb(rb,gb,bb)">
<p id="battery"></p>
</font>
Nothing happens also. 也没有任何反应。 How do I make the variables carry across the files so I can change the color of my battery to three different colors depending on what "level" is equal to?
如何使变量携带在文件中,以便根据“级别”等于什么将电池的颜色更改为三种不同的颜色? I have tried each combination of script, but I'm a complete frootloop when it comes to modifying someone else's code.
我已经尝试过每种脚本组合,但是在修改其他人的代码时,我是一个完整的frootloop。 Can you help me out?
你能帮我吗? Thanks in advance!
提前致谢!
Make three CSS classes with the three colors. 用三种颜色制作三个CSS类。
Change the class of the element with js in the conditionals. 在条件语句中使用js更改元素的类。 Here is how to do it: Change an element's class with JavaScript
方法如下: 使用JavaScript更改元素的类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.