XML内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<user-mdx>
<zzcb>
<slje id="1" name="数量">select * from TB_ZZBB</slje>
</zzcb>
</user-mdx>我使用document.getElementById("1")想取到该元素,但是取不到,提示取出的是一个null对象,这是什么原因,getElementById方法应该怎么用?id只能是标签的属性吗?
<?xml version="1.0" encoding="UTF-8"?>
<user-mdx>
<zzcb>
<slje id="1" name="数量">select * from TB_ZZBB</slje>
</zzcb>
</user-mdx>我使用document.getElementById("1")想取到该元素,但是取不到,提示取出的是一个null对象,这是什么原因,getElementById方法应该怎么用?id只能是标签的属性吗?
Element getElementById(String elementId)Returns the Element that has an ID attribute with the given value. If no such element exists, this returns null . If more than one element has an ID attribute with that value, what is returned is undefined.
The DOM implementation is expected to use the attribute Attr.isId to determine if an attribute is of type ID.
Note: Attributes with the name "ID" or "id" are not of type ID unless so defined.
Parameters:
elementId - The unique id value for an element.
Returns:
The matching element or null if there is none.
Since:
DOM Level 2 注意那个note,你没有在dtd或是schema中定义Id是个标识符的话,是不被识别的。
建议使用getElementsByTagName(String tagname),或是遍历该DOM树