如何使用JavaScript访问自定义XHTML属性?

tpo*_*wer 1 javascript xhtml attributes

我有以下XHTML:

<span id="myid" cus:att="myvalue" xmlns:cus="http://mycompany.com/customnamespace">
</span>
Run Code Online (Sandbox Code Playgroud)

是否可以使用javascript访问自定义属性?我有代表跨度的元素.做myElement.att不行,我无法弄清楚如何指定命名空间?

ann*_*ata 5

通常情况下,您可以直接访问它,即element.attribute命名空间稍微复杂化为:

element.getAttribute("namespace:attribute") //the nuclear x-browser option
Run Code Online (Sandbox Code Playgroud)

所以,为了真正清楚,这将是这样的:

document.getElementById('myid').getAttribute('cus:att')
Run Code Online (Sandbox Code Playgroud)