将 id 添加到中标签不起作用
我正在尝试使用 jQuery 的 .attr()
将 id 属性添加到
标记:
r.circle(x, y, 6).attr({fill: "#ff0000", stroke: "none", id: "cir1"}),
问题是它添加了 fill 和
lines
属性,但不是 id
。我知道,因为我查看了 Google Chrome DEV,它没有显示 id
!
I'm trying to add an id attribute to the <circle>
tag using jQuery's .attr()
:
r.circle(x, y, 6).attr({fill: "#ff0000", stroke: "none", id: "cir1"}),
The problem is that it adds the fill
and stroke
attributes but not id
. I know because I look at the Google Chrome DEV and it's not showing the id
!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据文档,您无法使用
attr
添加id
。请参阅此处:http://raphaeljs.com/reference.html#attr
如果您需要< code>id 作为一个钩子,也许你可以设置一个
title
,这是允许的。旁白:
很难从文档中判断是否允许这样做,但也许您可以通过将
id
放在引号中将其添加为自定义attr
?According to the documentation, you can't add an
id
usingattr
. See here:http://raphaeljs.com/reference.html#attr
If you need the
id
as a hook, perhaps you could set atitle
, which is allowed.An aside:
It's hard to tell from the docs if this is allowed, but maybe you can add the
id
as a customattr
by placing it in quotes?这对我来说效果很好。
证明:打开此测试页面并打开您的开发者控制台。您将在输出中看到
"yay 1 Circle"
,这是此 JavaScript 代码的结果:从中您可以看到
id
属性 a) 已被设置在 DOM 元素上,b) 作为通过 jQuery 或 DOM 方法访问元素的方法。您能否提供一个有效的测试用例(也许使用 Raphael)来显示此失败?
This works fine for me.
Proof: open this test page and open your developer console. You will see
"yay 1 circle"
in the output, which is the result of this JavaScript code:From this you can see that the
id
attribute is both a) being set on the DOM element, and b) working as a means for accessing the element through either jQuery or DOM methods.Can you provide a working test case, perhaps using Raphael, showing this failing?
显然,你不能通过 attr 设置 id。
只需获取节点引用并直接设置即可。
Apparently, you can't set the id thru attr.
Just get the node reference and set it directly.
在某些情况下,您不需要 ID。
您可以使用关键字
this
。In some instances you do not need an ID.
You can use the keyword
this
.