2013-10-21 138 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

你修改了什麼? –