2013-06-11 64 views
0

我想設置一個ID拉斐爾集,因爲我想一個事件點擊後顯示它。設置一個ID爲「Raphaeljs設置」

這是我的設置:

var divResult = document.getElementById('printResult'); 
var space2Draw = Raphael(divResult, 1600, 900); 
var st = space2Draw.set(); 

st.push(
    space2Draw.circle(newXCoordinates, newYCoordinates, 20).click((function (valore) { 
     return function() { 
      window.open("index-point.html?id=" + (valore) + "&type=" + type + "&description=" + description + "&name=" + name); 
     } 
    }(valore))).mouseover(function() { 
     this.attr({ 'cursor': 'pointer' }); 
     this.attr({ 'opacity': '.50' }); 
    }).mouseout(function() { 
     this.attr({ 'opacity': '1' }); 
    }) 

      ); 
在我的網頁

我有一個按鈕:

function show(){ 
     var element = space2Draw.getById(-1); 
     element.show(); 

    } 
} 

是不是可以設置一個ID這樣:set.id = -1? 我該如何設置一個ID然後找到該設置?

在此先感謝您的幫助。

回答

1

也許你可以使用Raphael data()功能的任何數據分配給你的元素/套。

例子:

// you can keep global var for your id and increment it within your code 
var id = 0; 

var p = Raphael(100, 100, 500, 500), 
    r = p.rect(150, 150, 80, 40, 5).attr({fill: 'red'}), 
    c = p.circle(200, 200, 70).attr({fill: 'blue'}); 

var set = p.set(r,c).data("id", id); 
id++; 

// then in your click event to get the id you can do 
var whichSet = this.data("id"); 

// data("id") will return you the global id variable 

好運

2

您可以嘗試使用setAttribute()添加類以後要訪問的元素,然後修改自己的CSS propperties。第一步是改變每個元素的CLASS:

myElement.node.setAttribute("class", "class_name"); 

不幸的是,拉斐爾不允許您處理集作爲獨特的HTML對象,所以你不能這樣做對整個集一次。相反,你可能有一個這樣做的每個元素在你的設置,或者爲週期,這樣的事情:

for (var i=0; i<st.length; i++) { 
    st[i].node.setAttribute("class", "class_name"); 
} 

然後,使用JQuery,你可以修改你爲了創建的類的CSS屬性顯示您的設置中的元素。

function show(){ 
    $('.class_name').css('display', 'block'); 
} 

我希望這會有所幫助。

+0

我嘗試過:st.node.setAttribute(「id」,「element_id」),bur我有一個錯誤:Uncaught TypeError:無法調用未定義的方法'setAttribute' – ilamaiolo

+0

我已經重寫了我的答案來解釋這一點。看起來Raphael不能將這個集合當作一個對象,所以setAttribute方法是不適用的。相反,您需要更改集合中的每個對象(併爲其分配一個CSS類)。請參閱上文以獲取更完整的解釋。 – gatotkaca