如何在拉斐尔中获取绘制的矩形元素

How to get the drawn rectangle element in Raphael?

本文关键字:绘制 元素 获取      更新时间:2023-09-26

我想知道使用Raphael绘制元素的方法是什么.js.我尝试使用getById,但它根本不起作用。请帮助我克服这个问题。

当用户单击圆圈时,我正在获取圆 ID 并希望获取//矩形,该//矩形也与圆具有相同的 ID,但前缀不同。

function addCircleClick(obj)
{
    obj.click(function(event)
    {
        alert(paper+" getRect ID 1"+this.id);.
        var getRect; 
        try
        {
            var getRect  = paper.getById(this.id);////This below line(paper.getById(this.id)) is not working,even Circle object i am not able  to get
        }
        catch(e)
        {
            alert(e);//Issue here
        } 
        alert("getRect ID 2 "+getRect); 
        obj.attr({"stroke":"red"});  
    });
}       

我认为您的问题是您正在尝试使用示例中所示的纸张。希望这对您有所帮助。

var paper = Raphael("field1", 240, 400, actions);
var attrs = {
        fill: "#FFF"    
    };
function actions() {
    var that = this;  
    var circle = that.circle(50,50,10);
    circle.attr(attrs);
    circle.click(function(event) {
        console.log(this.id); //elements id
        console.log(that.getById(this.id)); //clicked element
        console.log(paper.getById(this.id)); //doesn't work
    });
};

编辑:重读你的问题,认为我可能误解了它。无论如何,我不太确定您获得具有不同前缀的相同id是什么意思。每个元素都获得唯一的数字 ID。