以下是jQuery版本的需求
:
http://jsbin.com/utepi3
$(function() {
var counter = 0;
for(var i = 0;i< 5; i++) {
for(var k = 0; k < 5; k++) {
var img = $('<img />', {
id : 'i'+i+'k'+k,
src: 'http://mclightning.com/gr.jpg',
width : 30,
height : 30,
alt : 'test',
value : '0'
}).appendTo('body').wrap('<div class="hello"/>');
$('.hello').eq(counter).css({
position : 'absolute',
left: i*30,
top : k*30,
cursor : 'pointer'
});
counter++;
}
}
$('img').hover(function() {
if($(this).attr('value') != 1) {
$(this).attr('src', 'http://mclightning.com/grover.jpg');
}
},function() {
if($(this).attr('value') != 1) {
$(this).attr('src', 'http://mclightning.com/gr.jpg');
}
}).bind('click',function() {
if($(this).attr('value') !='1' ) {
$(this).attr('src', 'http://mclightning.com/grclick.jpg');
$(this).attr('value','1');
}else if($(this).attr('value') !='0' ) {
$(this).attr('src', 'http://mclightning.com/gr.jpg');
$(this).attr('value','0');
}
});
});
附言