You can add a background color style to the element when clicked, and use the setTimeout
function to remove it later:
$(function(){
$('li').click(function(){
var e = $(this);
e.css('background-color', 'red');
window.setTimeout(function(){
e.css('background-color', '');
}, 500);
});
});
(Note: There is a delay
method in jQquery, but that is intended to be used with animation, not as a replacement for setTimeout
.)
Working example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="sv" xml:lang="sv">
<head>
<title>Test</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('li').click(function(){
var e = $(this);
e.css('background-color', 'red');
window.setTimeout(function(){
e.css('background-color', '');
}, 500);
});
});
function calc(digit) {
var lcd = $('#lcd');
lcd.text(lcd.text() + digit);
}
</script>
</head>
<body>
<div id="lcd"></div>
<ul>
<li class="key" onClick="calc('1')">1</li>
<li class="key" onClick="calc('2')">2</li>
<li class="key" onClick="calc('3')">3</li>
<li class="key" onClick="calc('4')">4</li>
<li class="key" onClick="calc('5')">5</li>
<li class="key" onClick="calc('6')">6</li>
<li class="key" onClick="calc('7')">7</li>
<li class="key" onClick="calc('8')">8</li>
<li class="key" onClick="calc('9')">9</li>
<li class="key" onClick="calc('0')">0</li>
</ul>
</body>
</html>