2013-10-21 80 views
0

我正在处理显示与文本文件中指定的颜色匹配的彩色按钮的代码。不过,我面临的一个问题是,只有第一个'if'在与条目匹配之后执行并且最后一个doesnt中指定的'if'条件完全被检查。 喜欢,如果指定的颜色是红色,蓝色。然后输出只显示红色的按钮。多个如果条件不起作用

这里是我的代码

if(R) 
{ 
document.write('<button style="background-color:red;width:100;height:100" </button>'); 
} 

else 

if(V) 
{ 

    document.write('<button style="background-color:violet;width:100;height:100"  
    </button>'); } 

else 

if(I) 
{ 
document.write('<button style="background-color:indigo;width:100;height:100" 
</button>'); 
} 


else 

    if(B) 
{ 

document.write('<button style="background-color:blue;width:100;height:100" 
    </button>'); 
} 

else 

if(G) 
{ 

document.write('<button style="background-color:green;width:100;height:100" 
    </button>'); 
    } 

    else 

    if(Y) 
    { 

document.write('<button style="background-color:yellow;width:100;height:100" 

</button>'); 
} 


else 

if(O) 
{ 
document.write('<button style="background-color:orange;width:100;height:100" 

</button>'); 
} 
+0

这些变量的值是什么?你在控制台中遇到任何错误吗?你可以做一个'''FIDDLE'''吗? –

+0

所以你假设javascript会为你猜'R','I','B'和'V'的含义?幸运的是,它还不能这样做(嗨,SkyNet) –

+4

如果你想做多个不相关的测试,那么你根本不想使用'else'。另外:***代码格式化!! *** –

回答

1

使用CSS,而不是styleclass上课。尽量不要使用document.write(除非您使用的是单页架构)。相反,把你的HTML在正确的地方:

<button id='mybutton' class='colored'></button> 

,并在你的CSS文件:

.colored { 
    width: 100; 
    height: 100; 
    background-color: puce; /* or some other default */ 
} 

然后在你的Javascript:

var button = document.getElementById('mybutton'); 
if (button) { button.backgroundColor=calculatedColor(...); } 

其中calculatedColor是一个函数,在if声明中执行条件的工作。

JQuery有快捷方式,但您应该首先使用标准的Javascript。

0

那么,你的代码编写完全一样的。一旦条件匹配,其余的被忽略。这就是为什么else。如果您想要评估所有条件,请尝试删除else

1

如果你想打印多色彩按钮,那么你不应该使用else。试试这个

if(R) 
{ 
    document.write('<button style="background-color:red;width:100;height:100" </button>'); 
} 

if(V) 
{ 
    document.write('<button style="background-color:violet;width:100;height:100"  
    </button>'); 
} 
if(I) 
{ 
    document.write('<button style="background-color:indigo;width:100;height:100" 
    </button>'); 
} 
if(B) 
{ 
    document.write('<button style="background-color:blue;width:100;height:100" 
    </button>'); 
} 
if(G) 
{ 
    document.write('<button style="background-color:green;width:100;height:100" 
    </button>'); 
} 
if(Y) 
{ 
    document.write('<button style="background-color:yellow;width:100;height:100" 
    </button>'); 
} 
if(O) 
{ 
    document.write('<button style="background-color:orange;width:100;height:100" 
    </button>'); 
} 

而且文件撰写取代以前的值,从而尝试添加元素

0

嗨,

请尝试以下...这是修改后的代码行....

if(R) 
{ 
    document.write('<button style="background-color:red;width:100;height:100"></button>'); 
} 
else if(V) 
{ 
    document.write('<button style="background-color:violet;width:100;height:100"></button>'); 
} 
else if(I) 
{ 
    document.write('<button style="background-color:indigo;width:100;height:100"></button>'); 
} 
else if(B) 
{ 
    document.write('<button style="background-color:blue;width:100;height:100"></button>'); 
} 
else if(G) 
{ 
    document.write('<button style="background-color:green;width:100;height:100"></button>'); 
} 
else if(Y) 
{ 
    document.write('<button style="background-color:yellow;width:100;height:100"></button>'); 
} 
else if(O) 
{ 
    document.write('<button style="background-color:orange;width:100;height:100"></button>'); 
} 

谢谢 维沙尔帕特尔

+0

你修改了什么? –