jQuery delegate/on/live/bind & random color generate
by fangunxs
HTML
<div style="width:200px;height:200px;background-color:black;"></div>
JavaScript
var id = 2;
var width = 200;
var height = 200;
$( "body" ).delegate( "div", "click", function() {
//console.log(this);
var child = document.createElement('div');
child.style.width = width/id + 'px';
child.style.height = height/id+'px';
child.style.backgroundColor = get_random_color();
$( this ).append(child);
id++;
return false;
});
function get_random_color() {
var letters = '0123456789ABCDEF'.split('');
alert(letters);
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.round(Math.random() * 15)];
}
return color;
}