Raphael.js属性和VML

时间:2012-10-11 10:11:00

标签: javascript attributes svg raphael vml

使用Raphael我想创建一个带有Id属性的矩形,如下例所示。

<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);

并使用类似

的代码设置Id
elem[0].setAttributeNS(null, 'id', 'aRect');

或使用此类代码

elem.node.id = 'aRect';

现在raphael回归到旧版IE上的vml,如何添加一个也适用于vml案例的id属性,或者这段代码也适用于此?

1 个答案:

答案 0 :(得分: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;
    }
}