2014-02-23 74 views

回答

1

在節點上設置的所有屬性都在yourObject.attrs對象中。

赫雷什示例代碼:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Prototype</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script> 
<style> 
body{padding:20px;} 
#container{ 
    border:solid 1px #ccc; 
    margin-top: 10px; 
    width:350px; 
    height:350px; 
} 
</style>   
<script> 
$(function(){ 

    var stage = new Kinetic.Stage({ 
     container: 'container', 
     width: 350, 
     height: 350 
    }); 
    var layer = new Kinetic.Layer(); 
    stage.add(layer); 

    var circle1 = new Kinetic.Circle({ 
     x:100, 
     y:100, 
     radius: 30, 
     fill: 'red', 
     stroke: 'black', 
     strokeWidth: 4, 
     draggable: true 
    }); 
    layer.add(circle1); 
    layer.draw(); 

    enumerateAttributes(circle1); 

    function enumerateAttributes(node){ 
     var text=""; 
     var attrs=circle1.getAttrs(); 
     for (key in attrs) { 
      if (attrs.hasOwnProperty(key)) { 
       text+=(key+"=="+attrs[key]+"\n"); 
      } 
     } 
     alert(text); 
    } 

}); // end $(function(){}); 

</script>  
</head> 
<body> 
    <div id="container"></div> 
</body> 
</html> 
+0

確實的obj.getAttrs()返回一個數組或一個字符串? – bigt95nt0110

+0

是不是要求跟進問題的「添加評論」? – bigt95nt0110

+0

它返回一個對象。 – markE