我想知道使用 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。
关于javascript - 如何在Raphael中获取绘制的矩形元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10688685/