getElementById for XML Documents, Mozilla extensions

最后都变了- 提交于 2019-12-06 01:17:27

No, document.getElementById doesn't generally work on arbitrary XML documents.

In recent browsers (e.g. Firefox 3.5 and later), you can use document.querySelector instead:

var resultNode = doc.querySelector("[id=" + id + "]");

If you would like a solution that actually makes the getElementById() method usable (which you should, It is much much faster, and more versatile) and you have access to the DTD add the following line to it:

<!ATTLIST client id ID #IMPLIED >

If you aren't using a DTD yet, just add this to the xml document directly after the <?xml version= \"1.0\"?> statement:

<!DOCTYPE clients [ 
   <!ATTLIST client id ID #IMPLIED > 
]>

In this example "clients" is the root of my xml file, and "client" is the element i would like to attach id's to. You can add multiple "ATTLIST" statements for other elements you would like to add id's to other types of elements.

I tested this in safari, chrome, and firefox. works perfectly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!