如何访问Raphael中任何元素的id属性

发布于 2024-10-07 15:28:18 字数 463 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念﹏祤嫣 2024-10-14 15:28:18

您确定不想写 $(t.node).attr('id','Hello'); 吗?

更新:有人刚刚否决了这个答案。我确实有义务指出这种设置 id 的方式并不是特别好。你最好使用:

t.node.id = 'Hello';

我希望有一种方法可以归功于胡安·门德斯,而不是赞成他对此答案的评论。

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.

梦回梦里 2024-10-14 15:28:18

试试这个:

function createLine()  { 
    var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
    t.attr('stroke-width','3');
    t.id = 'Hello';
    t.node.onclick = processPathOnClick;
}

function processPathOnClick() {
    alert($(this).id);
    alert(this.id); // This should work too...
}

基本上,您正在拉斐尔线实例变量“t”上创建一个名为“id”的新属性。在我看来,这有点像黑客攻击,但效果很好。

Try this:

function createLine()  { 
    var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
    t.attr('stroke-width','3');
    t.id = 'Hello';
    t.node.onclick = processPathOnClick;
}

function processPathOnClick() {
    alert($(this).id);
    alert(this.id); // This should work too...
}

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.

素罗衫 2024-10-14 15:28:18

尝试使用 jquery 设置处理程序

function createLine() 
{ 
  var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
  t.attr('stroke-width','3');
  t.attr('id','Hello');
  $(t.node).click(processPathOnClick);
}

function processPathOnClick() 
{
    alert($(this).attr("id"));
}

Try setting the handler using jquery

function createLine() 
{ 
  var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
  t.attr('stroke-width','3');
  t.attr('id','Hello');
  $(t.node).click(processPathOnClick);
}

function processPathOnClick() 
{
    alert($(this).attr("id"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文