你使用:
var item = layer.get('.rect1'+rect_counter); //dont user "+rec_counter" as this is what binds it to the LAST element
item.on('click', function(){
item.setFill('RED');
});
嘗試一個更基本的解決辦法:--------這一個最適合我---- -------
var itemsList = layer.getChildren(); //gets all shapes in this layer
for(var i=0; i<itemsList.length; i++){
if(itemsList.getName == 'rect1'){ //since you named your rectangles 'rect1' check name
itemsList[i].on('click', function(){
item.setFill('RED');
});
}
};
或簡單地嘗試只使用:
var item = layer.get('.rect'); //all rectangles, without number
item.on('click', function(e){ // I think you need an 'e' here
item.setFill('RED');
});
// I doubt this will work as ALL your rectangles need to have the same name if you want to .get() them all
你的jsfiddle不起作用,因爲你沒有對kinetic.js和kode.js的引用 – SoluableNonagon