2012-07-22 95 views
0

我做了一个菜单,其中有8个启用/禁用javascript函数按钮,我使用相同的JavaScript代码来运行所有8个按钮,但使用不同的css类为他们,所以我可以让他们上班。我的问题是,如何让他们工作到何处,当你点击他们的每一个功能。多个javascript按钮(启用/禁用)

JavaScript代码:我用这个代码7次以上与RED1红2等绿1 Green2的等

// Left Menu function controls(className) { 
if (className == "red") { 
    document.getElementById('Skip').setAttribute('class','green'); 
    // You can define your play music statements here 
} else { 
    document.getElementById('Skip').setAttribute('class','red'); 
    // You can define your stop music statements here   
} } 

if (className == "red1") { 
document.getElementById('Text').setAttribute('class','green1'); 
// You can define your play music statements here } else { 
document.getElementById('Text').setAttribute('class','red1'); 
// You can define your stop music statements here   } } 

CSS代码:相同的代码对所有8个按钮.red1 .red2等.green1 .green2等

.red { 
background: red; 
height: 30px; 
width: 30px; 
border-radius: 50px; 
border: 1px solid #000;}                        

.green { 
background: green; 
height: 30px; 
width: 30px; 
border-radius: 50px; 
border: 1px solid #000;} 

HTML代码:等

Skip &nbsp; <button id="Skip" class="red" onclick="controls(this.getAttribute('class'))">Play</button> 

Text &nbsp; <button id="Text" class="red1" onclick="controls(this.getAttribute('class'))">Play</button> 

和6多个按钮与相同的码RED2 RED3。

回答

0

我认为你应该添加id到你的按钮,id指的是你想播放或停止的音乐。

+0

我的Javascript ID document.getElementById('Skip')。和我的按钮ID ID =“跳过”。它仅适用于一个JavaScript代码和按钮。 – akamerc 2012-07-22 20:42:39

+0

演示[http://jsfiddle.net/xQ3PT/42/](http://jsfiddle.net/xQ3PT/42/)当您单击第一个时,第二个会变绿,但它们都需要在你点击变成红绿色。 – akamerc 2012-07-22 21:20:14