2012-10-11 34 views
0

随着Raphael我想创建一个像下面的例子一样的Id属性的矩形。Raphael.js属性和VML

<rect id="aRect" x="10" y="10" width="50" height="50" r="2" rx="2" ry="2"/> 

要创建我可以用这样的代码

var elem = _paper.rect(10, 10, 50, 50, 2); 

的矩形,并使用这样

elem[0].setAttributeNS(null, 'id', 'aRect'); 

或像这样

elem.node.id = 'aRect'; 
代码代码设置ID

现在拉斐尔退回到vml在较老的IE浏览器的权利,我怎么能添加一个id属性,也迎合vml的情况下,或者这个代码的工作呢?

回答

0

在阅读MS页面here后,我实现了用于设置Id的此解决方案。

function setId(el, id){ 
    if(el === null || typeof el !== 'object') return; 
    if(Raphael.type === 'SVG') { 
     el[0].setAttributeNS(null, 'id', id); 
    } 
    else if(Raphael.type === 'VML') { 
     el[0].id = id; 
    } 
}