如何访问Raphael中任何元素的id属性 [英] How to access id attribute of any element in Raphael
问题描述
我正在使用Raphael在网站上绘制一些元素。元素包括矩形,线(路径)。我给了path元素一个id,并尝试在该行的onclick事件中访问它。但是当我对id进行警报时,看不到任何东西。以下是代码段
function createLine()
{
var t = paper.path( M+ xLink ++ yLink +L+ linkWidth ++ linkHeight);
t.attr('stroke-width','3');
t.attr('id','你好');
t.node.onclick = processPathOnClick;
}
函数processPathOnClick()
{
alert($(this).attr(id));
}
任何人都可以告诉我上面的代码有什么问题。任何指针都会有所帮助。
谢谢
你确定吗?你不想写 $(t.node).attr('id','Hello');
而不是?
更新:有人对这个答案进行了低估。而且我真的觉得有必要指出这种设置id并不是特别好的方式。你最好使用:
t.node.id ='你好';
我希望有一种方法可以归功于Juan Mendes,除了赞成他对这个答案的评论。 / p>
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
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:
t.node.id = 'Hello';
I wish there was a way to credit Juan Mendes, other than upvoting his comment to this answer.
这篇关于如何访问Raphael中任何元素的id属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!