gpt4 book ai didi

javascript - Raphaël JS paper.getById() 返回 null

转载 作者:行者123 更新时间:2023-11-28 19:57:44 24 4
gpt4 key购买 nike

我尝试使用 getById() 函数获取 Raphaël 对象,但是无论我尝试什么,该函数都会返回 null。

这是我正在使用的 Raphaël JS 代码:

// Creates canvas 320 × 200 at 10, 50
var paper = Raphael(10, 50, 320, 200);

var circle = paper.circle(100, 100, 100);
circle.attr({"fill": "#f00", "id": "test"});
circle.data("id", "test");

var getCircle = paper.getById("test");

alert(getCircle); //Returns null?

为什么paper.getById("test");不返回圆对象?

我已经使用 attr()data() 定义了 ID,但似乎没有任何效果。

上述代码的JsFiddle:http://jsfiddle.net/Mf9q6/

最佳答案

阅读documentation仔细:

Returns you element by its internal ID.

这与元素上的 id 属性不同。 Paper.getById() 期望的 id 是 Element.id

您可能想要 var getCircle = paper.getById(circle.id); 无论如何,它只是对与 circle 相同的对象的引用。

Here's your fiddle, updated .

关于javascript - Raphaël JS paper.getById() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22234197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com