简体   繁体   English

我需要在.js文件,.html和.css文件之间传递的变量。 我该怎么做呢?

[英]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.

相关问题 如何将变量从PHP传递到外部JS文件? - How do I pass variables from PHP to an external JS file? 如何将 CSS 和 js 文件捆绑到一个 HTML 文件中? - How do I bundle CSS and js files into a single HTML file? 如何将变量/数据从 JS 文件传递到 HBS 文件? - How do I pass variables/data from a JS file to an HBS file? 量角器打字稿在tsConfig文件中将ts转换为js文件时出现问题。 我该如何解决? - Protractor Typescript Problem converting ts to js file in tsConfig file. How do I fix this? 我正在创建一个Mac应用程序,该应用程序需要读取和写入HTML文件。 我该如何实现? - I'm creating a Mac application that needs to read and write to/from an HTML file. How do I achieve this? 当我单击按钮时,我在html中有textarea,我需要将textarea内的内容另存为pdf文件。 我该怎么做? - im having textarea in html when i click the button i need to save the content inside the textarea as pdf file. how can i do it? 如何在函数之间传递变量? - how do I pass variables between functions? 我需要读取用TypeScript(An Angular Module)编写的文件,并以编程方式将代码添加到原始文件中。 关于我该怎么做的任何想法? - I need to read a file written in TypeScript (An Angular Module) and programatically add code to the original file. Any ideas on how can I do it? 在另一个JS文件中调用全局函数。 我如何获得“这个”的作用? - Calling a global function in another JS file. How do I get 'this' to work? RequireJS:如何将变量从一个文件传递到另一个文件? - RequireJS: How do I pass variables from one file to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM