简体   繁体   English

XHTML5中的SVG:使用适当的命名空间设置属性

[英]SVG in XHTML5: setting attributes with proper namespace

TL;DR Summary : Is it proper to use setAttribute instead of setAttributeNS for SVG elements? TL; DR摘要 :对SVG元素使用setAttribute而不是setAttributeNS是否合适?

Details : 细节
Consider this SVG image embedded in XHTML5 that uses JavaScript to dynamically create and add elements to the drawing: http://phrogz.net/svg/svg_in_xhtml5.xhtml 考虑嵌入在XHTML5中的这个SVG图像,它使用JavaScript动态创建和添加元素到图形: http//phrogz.net/svg/svg_in_xhtml5.xhtml

The SVG elements created by JavaScript and appended to the <svg> element must be created using... 必须使用JavaScript创建由JavaScript创建并附加到<svg>元素的<svg>元素。

var el = document.createElementNS("http://www.w3.org/2000/svg",'foo');

...instead of... ...代替...

var el = document.createElement('foo');

...in order for them to be treated as SVG elements and rendered in the browser. ...以便将它们视为SVG元素并在浏览器中呈现。 This is reasonable and understandable. 这是合理的,也是可以理解的。 However, according to this page I should also be setting the attributes of these elements using... 但是,根据这个页面,我还应该使用...设置这些元素的属性。

el.setAttributeNS( null, 'foo', 'bar' );

...instead of the code I'm using currently: ...而不是我目前使用的代码:

el.setAttribute( 'foo', 'bar' );

What I am doing works in Chrome, Safari, and Firefox. 我在做什么适用于Chrome,Safari和Firefox。 Is what I have legal code —is it equivalent to the recommendation—or does it just happen to work due to the permissive nature of browsers and I must instead use setAttributeNS to be valid? 我有合法代码 - 它是否等同于推荐 - 或者它是否因为浏览器的宽容性而恰好工作,我必须改为使用setAttributeNS才有效?

As long as you don't use namespaced attributes (with or without a prefix) you can use setAttribute. 只要不使用命名空间属性(带或不带前缀),就可以使用setAttribute。

The setAttributeNS recommendation is good in a way, because then you don't need to worry about using different methods (and when to use which one). setAttributeNS建议在某种程度上是好的,因为那样你就不必担心使用不同的方法(以及何时使用哪种方法)。 You really only need setAttributeNS when you need to change eg xlink:href or custom namespaced attributes. 当您需要更改例如xlink:href或自定义命名空间属性时,您实际上只需要setAttributeNS。 On the other hand people do get the namespaces wrong (sometimes trying to use eg the svg namespace instead of NULL for svg attributes), so it's not clear what is less confusing IMHO. 另一方面,人们确实错误地命名空间(有时尝试使用例如svg命名空间而不是svg属性的NULL),所以不清楚什么是不那么令人困惑的恕我直言。

DOM 2 Core says this about the DOM Level 1 get/setAttribute methods: DOM 2 Core说这是关于DOM Level 1 get / setAttribute方法的:

DOM Level 1 methods are namespace ignorant. DOM Level 1方法是名称空间无知。 Therefore, while it is safe to use these methods when not dealing with namespaces, using them and the new ones at the same time should be avoided. 因此,虽然在不处理命名空间时使用这些方法是安全的,但应避免同时使用它们和新的命名空间。

I might add that "at the same time" should maybe have read "at the same time on the same (intended) attribute", or something similar. 我可能会补充一点,“同时”应该“同时读取同一个(预期)属性”或类似内容。

SVG itself doesn't require your scripts to "be legal" or anything like that except for the svg markup itself, but it does require support for certain DOM specifications, such as DOM 2 Core in the case of SVG 1.1. SVG本身并不要求你的脚本“合法”或类似的东西,除了svg标记本身,但它确实需要支持某些DOM规范,例如在SVG 1.1的情况下DOM 2 Core。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM