[英]How to add CSS code within if…else if…else Javascript statement
I'm trying to create an if...else if...else statement in Javascript that will change the style of a specific CSS ID based on a specific number. 我正在尝试在Javascript中创建一个if ... else if语句,它将根据特定的数字更改特定CSS ID的样式。 The if...else if...else code isn't the problem, it's just that as a Javascript novice (putting it lightly...) I'm not sure where to add the CSS code to execute it.
if ... else if ... else代码不是问题,它只是作为一个Javascript新手(轻描淡写......)我不知道在哪里添加CSS代码来执行它。 From what I understand I would put it in the areas I designated as "CSS CODE" below (sorry for the poorly formated code).
根据我的理解,我会将它放在我下面指定为“CSS CODE”的区域(对于编码不良的代码而感到抱歉)。
if (Number == 9) {CSS CODE} else if (Number == 8) {CSS CODE}
I looked at another question here and the code that was provided to achieve this is below. 我在这里看了另一个问题,为实现这个目的而提供的代码如下。
var element = document.getElementById("ElementID");
element.style.backgroundColor = "#fff";
When I placed the var at the top and the element.style.etc within the CSS CODE section it did not work properly. 当我将var放在顶部并且在CSS CODE部分中放置了element.style.etc时,它无法正常工作。
As a side note I need all the styling to be contained within the script, so adding a new class to it won't work. 作为旁注,我需要将所有样式包含在脚本中,因此向其中添加新类将不起作用。 Along the same line of thought I can't use jQuery for any of this or I would have already :)
沿着同样的思路我不能使用jQuery任何这个或我已经:)
This doesn't work? 这不起作用?
var element = document.getElementById("ElementID");
var Number = 8;
if (Number == 9) {
element.style.backgroundColor = "#fff";
} else if (Number == 8) {
element.style.backgroundColor = "#000";
}
var element = document.getElementById("ElementID");
var Number = 1;
switch(Number)
{
case 1:
element.style.backgroundColor = "#fff";
break;
case 2:
element.style.backgroundColor = "#000";
break;
default:
element.style.backgroundColor = "#FAB";
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.