如何在Chrome下使用javascript获取整个xml节点? [英] How can i get the entire xml node with javascript under Chrome?

查看:79
本文介绍了如何在Chrome下使用javascript获取整个xml节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var xml = '<books><book><id>1</id></book></books>';
var xmlDoc = $.parseXML(xml);
$(xmlDoc).find("book").each(function(){
  alert($(this));
});

我想看的是:

<book><id>1</id></book>

在IE中,我可以使用$(this)[0] .xml.但是在Chrome中,没有xml属性. 那我该怎么办?

In IE, i can use $(this)[0].xml. But in Chrome, there's no xml property. What should I do then?

谢谢.

推荐答案

最简单的方法是使用浏览器的内置XMLSerializer实现(包括IE 9在内的所有主要浏览器的当前版本中都提供):

The easiest way is to use the browser's built-in XMLSerializer implementation (present in the current versions of all major browsers, including IE 9):

function serializeXmlNode(xmlNode) {
    if (typeof window.XMLSerializer != "undefined") {
        return new window.XMLSerializer().serializeToString(xmlNode);
    } else if (typeof xmlNode.xml != "undefined") {
        return xmlNode.xml;
    }
    return "";
}

var xml = '<books><book><id>1</id></book></books>';
var xmlDoc = $.parseXML(xml);
$(xmlDoc).find("book").each(function(){
    alert( serializeXmlNode(this) );
});

参考:

  • W3C spec
  • MDN
  • IE 9 support

这篇关于如何在Chrome下使用javascript获取整个xml节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆