Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. Web API
  3. Element
  4. Element:setAttributeNS() 方法

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

Element:setAttributeNS() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

setAttributeNS 根据给定的命名空间和名称添加一个新属性或改变属性的值。

如果你正在处理 HTML 文档,并且你不需要指定要设置的属性属于特定的命名空间,那么使用Element.setAttribute() 方法即可。

语法

js
setAttributeNS(namespace, name, value)

参数

  • namespace:一个指定属性的命名空间的字符串。
  • name:一个标识属性的限定名称的字符串。即一个命名空间前缀,后跟一个冒号,再跟一个本地名称。
  • value:一个表示新的属性值的字符串。

返回值

无(undefined)。

示例

js
let d = document.getElementById("d1");d.setAttributeNS(  "http://www.mozilla.org/ns/specialspace",  "spec:align",  "center",);

备注

在限定命名空间的属性的方法中,setAttributeNS 是唯一一个需要完全限定名(即"namespace:localname")的方法。

规范

Specification
DOM
# ref-for-dom-element-setattributens①

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp