2012-06-25 55 views
0

我已經分配了一個自定義的數據屬性添加到拉斐爾畫布一些圈子作爲一個each()循環如下:jQuery的 - 拉斐爾 - SVG - 獲取元素的位置用相同的數據屬性到數組

marker.data('transaction', transaction); 

怎麼辦我在畫布上找到具有相同事務數據值的元素,並將其位置置於for循環中的數組中,然後對該數組執行操作?

例如,在僞代碼:

for (current_transaction = 1; current_transaction < 10; current_transaction++) { 
    var array = find the location of elements with transaction data value of 
    current transaction; 
    //perform some function with that array 
} 

我的問題是真的話,你怎麼「getElementByData」,然後搜索使用相同的數據元素。其餘的很簡單。

回答

1

document.querySelectorAll('[transaction]')將得到與事務屬性相關的所有內容。

使用[transaction ='foo']來獲取具有值foo的交易屬性,例如,

<svg xmlns="http://www.w3.org/2000/svg" version="1.1"> 

    <path transaction="1"/> 
    <path transaction="2"/> 
    <path transaction="1"/> 

    <script> 
    for (var current_transaction = 1; current_transaction &lt; 2; current_transa 
ction++) { 
     var found = document.querySelectorAll("[transaction='" + current_transact 
ion +"']"); 
     alert(found.length); 
    } 
    </script> 

</svg> 
+0

因此,像http://pastebin.com/gnXf79MR – jacktheripper

+0

沒有,CURRENT_TRANSACTION是一個數字,所以你在做document.querySelectorAll(1) –

+0

VAR發現= document.querySelectorAll('[交易= CURRENT_TRANSACTION] 「); – jacktheripper