2013-01-18 22 views
0
<script type="text/ecmascript"> 
<![CDATA[ 
function setCoordinates(evt) { 
var centerX = Math.round(Math.random() * 1000); 
var centerY = Math.round(Math.random() * 1000);  
evt.target.setAttributeNS(null,"cx",centerX); 
evt.target.setAttributeNS(null,"cy",centerY); 
} 
]]> 
</script> 

這是我的功能。 我真的被困在如何解析(不知道這是否是正確的詞)結果的一個圈子的屬性。如何解析JS函數的結果到SVG對象?

比方說,我的圈子裏是這樣的:

<circle cx="0" cy="0" r="10" fill="red" /> 

什麼是需要的JavaScript調用的函數,因此解析屬性值的循環?

+0

你以後的是什麼不清楚。而且目前的代碼在做什麼也不清楚。你在做什麼? –

+0

http://jsfiddle.net/W5Ww5/? –

回答

2

我不認爲你想要一個事件參與。你可能想(如果你使用的null命名空間反正不需要setAttributeNS())像

function setCoordinates(circle) { 
    var centerX = Math.round(Math.random() * 1000); 
    var centerY = Math.round(Math.random() * 1000);  
    circle.setAttribute("cx",centerX); 
    circle.setAttribute("cy",centerY); 
} 

如果你給你的圈子的ID,像

<circle cx="0" cy="0" r="10" fill="red" id="myCircle"/> 

那麼你可以用代碼改變座標,如

setCoordinates(document.getElementById("myCircle")); 
+0

謝謝,這是一個非常明確和信息豐富的答案。正是我想要的:D –