Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > JavaScript: Общие вопросы > Как при помощи переключателей блокировать кнопку?


Автор: Win MK 32 27.10.2009, 11:31
Сам делал
Код
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
<title>Изменение цвета страницы на JS</title>
<style type="text/css">
div{
    border:1px dotted;
    margin:20px;
    padding:10px;
    text-align:justify;
    float:left;
    max-width:300px;
}
</style>
</head><body>

<div>
Страница, цвет которой изменялся бы при каждом третьем нажатии соответствующей кнопки, {b}при этом имелась бы возможность этот режим обработки нажатия кнопки отключать при помощи переключателя.{/b}
</div>

<div>
   <input type="button" value="Сменить цвет страницы" id="button" />
   <input name="status" type="radio" value="on" />Вкл
   <input name="status" type="radio" value="off" />Выкл
</div>

<script type="text/javascript">
var n;
n = 0;
document.getElementById('button').onclick = function(){
    n++;
    if(n % 3 == 0){
        document.bgColor="#"+genRandimHexColor();
    }
}
function genRandimHexColor(){ 
    colors = new Array(14) 
    colors[0]="0" 
    colors[1]="1" 
    colors[2]="2" 
    colors[3]="3" 
    colors[4]="4" 
    colors[5]="5" 
    colors[5]="6" 
    colors[6]="7" 
    colors[7]="8" 
    colors[8]="9" 
    colors[9]="a" 
    colors[10]="b" 
    colors[11]="c" 
    colors[12]="d" 
    colors[13]="e" 
    colors[14]="f" 
    
    digit = new Array(5) 
    color="" 
    for(i=0;i<6;i++){ 
        digit[i]=colors[Math.round(Math.random()*14)] 
        color = color+digit[i] 
    }
    return color;
}
</script>
</body></html>

Автор: EmilRegis 27.10.2009, 12:54
Код

document.getElementById('button').disabled = true;

Автор: bars80080 27.10.2009, 15:18
Win MK 32, а почему умножается на 14? f так никогда и не попадёт в цвет


Автор: Michael.de 27.10.2009, 19:25
bars80080, потому что там в строках 44-45
Код

 colors[5]="5" 
 colors[5]="6"
так будет короче:
Код

function genRandimHexColor(){
 // использовать colors - если переменная глобальная и var colors - если переменная локальная
 colors = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
 ...
 // P.S. и ставте плиз ; в конце каждой строки, хоть это пока и не обязательно

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)