代码之家  ›  专栏  ›  技术社区  ›  Alexey Kjetil Ytrehus

如何以跨浏览器的方式获取XML元素的属性?

  •  0
  • Alexey Kjetil Ytrehus  · 技术社区  · 16 年前

    Internet Explorer支持XML响应分析,允许您这样做

    element.xml.attributes.getNamedItem('myAttr')
    

    如何在基于标准的浏览器中对XML元素执行相同的操作?

    元素的类型是[对象元素]

    2 回复  |  直到 16 年前
        1
  •  0
  •   Quentin    16 年前

    使用 standard DOM instead of Microsoft's propriety approach. (Assuming you are talking about a proper XML document and not an IE4 era "XML data island")

        2
  •  0
  •   Community Mohan Dere    9 年前

    好的,使用Firebug从这里得到了答案: Versatile xml attribute regex with javascript

    element.ownerDocument.evaluate("//@"+attributeName, element.ownerDocument, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null).iterateNext().nodeValue;

            function GetAttributeValueFromXmlElement(element, attrName) {
            if (element && element.xml) {
                //msie
                return $(element.xml)[0].attributes.getNamedItem(attrName).value;
            } else {
                //standard DOM
                if (true/*Should check if there's only one here and only where expected to be*/) {
                    return element.ownerDocument
                        .evaluate("//@" + attrName, element.ownerDocument, null
                        , XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null)
                        .iterateNext().nodeValue;
                }
            }
        }