如何访问Raphael中任何元素的id属性
我正在使用 Raphael 在网站上绘制一些元素。元素包括矩形、线(路径)。我已经为路径元素提供了一个 id 并尝试在该行的 onclick 事件中访问它。但是当我对 id 发出警报时,什么都看不到。以下是代码片段
function createLine()
{
var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
t.attr('stroke-width','3');
t.attr('id','Hello');
t.node.onclick = processPathOnClick;
}
function processPathOnClick()
{
alert($(this).attr("id"));
}
有人可以告诉我上面的代码有什么问题吗?任何指针都会有帮助。
谢谢
I'm using Raphael for drawing some elements on a website. The elements include rectangle, line (path). I have given an id to the path element and trying to access it in the onclick event of that line. but when I do an alert of the id, nothing is visible. Following is the code snippet
function createLine()
{
var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
t.attr('stroke-width','3');
t.attr('id','Hello');
t.node.onclick = processPathOnClick;
}
function processPathOnClick()
{
alert($(this).attr("id"));
}
Can anyone please tell me what is the problem with the above code. Any pointer will be helpful.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定不想写
$(t.node).attr('id','Hello');
吗?更新:有人刚刚否决了这个答案。我确实有义务指出这种设置 id 的方式并不是特别好。你最好使用:
我希望有一种方法可以归功于胡安·门德斯,而不是赞成他对此答案的评论。
Are you sure you don't want to write
$(t.node).attr('id','Hello');
instead?Update: someone just downvoted this answer. And I truly feel obligated to point out this way of setting the id isn't particularly good. You would be better off using:
I wish there was a way to credit Juan Mendes, other than upvoting his comment to this answer.
试试这个:
基本上,您正在拉斐尔线实例变量“t”上创建一个名为“id”的新属性。在我看来,这有点像黑客攻击,但效果很好。
Try this:
Basically you are creating a new property called "id" on your Raphael line instance variable "t". It's kind of hacking, in my opinion, but it does the trick just fine.
尝试使用 jquery 设置处理程序
Try setting the handler using jquery