, jQuery (1.5.2).
, , , ?, - , . script div id on_off ( var!) class for on (on_off_on) off (on_off_off) , , jsFiddle
script -
HTML -
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.min.js" ></script>
</head>
<body>
<div id="on_off" class="on_off_on" >ON</div>
</body>
</html>
CSS -
div.on_off_off
{
background: rgb(150,110,120);
font-family: segoe ui;
font-size: 12px;
font-weight: bold;
padding: 5px 10px;
border: 3px solid rgb(180, 140, 150);
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
color: #fff;
display: inline;
}
div.on_off_on
{
background: rgb(110,150,120);
font-family: segoe ui;
font-size: 12px;
font-weight: bold;
padding: 5px 10px;
border: 3px solid rgb(140, 180, 150);
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
color: #fff;
display: inline;
}
jQuery -
$(document.body).ready(function () {
$(function () {
var main_obj_id = 'on_off';
var on_class = 'on_off_on';
var off_class = 'on_off_off';
$('#' + main_obj_id).click(function () {
if ($(this).is('.' + on_class)) {
$(this).removeClass(on_class);
$(this).addClass(off_class);
$(this).html('OFF');
} else {
$(this).removeClass(off_class);
$(this).addClass(on_class);
$(this).html('ON');
}
});
});
});
, !