問題描述
以null作為命名空間參數調用setAttribute和setAttributeNS有什么區別?
What is the difference between calling setAttribute and setAttributeNS with null as the namespace parameter?
使用 getAttribute() 然后 setAttributeNS 也有問題嗎?
Also is there an issue with using getAttribute() and then setAttributeNS ?
推薦答案
setAttribute() 是一個 DOM 1 函數.setAttributeNS() 是一個 DOM 2 函數,它通過在第一個參數中指定應該應用于標簽/屬性的 xmlns 命名空間來解決標簽或屬性名稱沖突的問題.
setAttribute() is a DOM 1 function. setAttributeNS() is a DOM 2 function that resolves the problem of conflicting tag or attribute names by specifying the xmlns namespace that should apply to the tag/attribute in the first argument.
如果屬性沒有定義的命名空間前綴,則第一個參數必須為 null.您可以使用 setAttribute(),但為了保持一致性,建議堅持使用 setAttributeNS().見:
If an attribute does not have a defined namespace prefix, the first argument must be null. You could use setAttribute() but for consistency it is advisable to stick to setAttributeNS(). See:
https://developer.mozilla.org/en/docs/Web/SVG/Namespaces_Crash_Course#Scripting_in_namespaced_XML
但是,請注意:XML 1.1 中的命名空間建議聲明沒有前綴的屬性的命名空間名稱沒有價值.換句話說,雖然屬性屬于標簽的命名空間,您不要使用標簽的命名空間名稱.相反,您必須使用 null 作為不合格的命名空間名稱(無前綴)屬性."
"However, note carefully: the Namespaces in XML 1.1 recommendation states that the namespace name for attributes without a prefix does not have a value. In other words, although the attributes belong to the namespace of the tag, you do not use the tag's namespace name. Instead, you must use null as the namespace name for unqualified (prefixless) attributes."
這篇關于setAttribute 和 setAttributeNS(null,的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!