2014-02-20 50 views
0

編輯:我想使用字符串rectString,因爲我將使用循環來獲取所有矩形並檢查是否有任何具有特定屬性。Kinetic.js使用字符串搜索ID

我有一組矩形,說他們的名字是'rect1','rect2'和'rect3'。我一直在嘗試搜索不同的方式來搜索我的舞臺,如:

var rectString="rect1"; 
var method1= stage.get(rectString)[0]; 
var method2= stage.get(rectString); 
var method3 =stage.find(rectString); 
var method4=node.getAttr(rectString); 

但不幸的是這些工作都不起作用。我正在嘗試獲取形狀的描邊顏色,然後使用形狀的ID對其進行更改。 感謝您的幫助

回答

0

這將讓舞臺上的所有矩形到一個集合:

var allRectangles=stage.find("Rect"); 

那麼你可以申請一個功能,每個矩形像這樣:

// run a function for each element in the allRectangles collection 

allRectangles.each(function(rect){ 

    // check if this rect has a red stroke 

    if(rect.stroke()=="red"){ 

     // if the stroke is red, change the stroke to blue 

     rect.stroke("blue"); 

    } 

}); 

layer.draw(); 
+0

感謝反饋,但我不是很清楚我的問題。我現在編輯它,但感謝您的答案! – billatron

+0

我編輯了我的答案,告訴你如何獲得舞臺上的所有矩形,並檢查是否有矩形具有特定的屬性。 – markE