IE能用jQuery操作XML吗?

时间:2022-12-07 00:17:18

What I am not trying to do:

我不想做的是:

  • Simply "read" XML in IE using jQuery. Been there, done that. Works for the most part.
  • 只需使用jQuery在IE中“读取”XML。我已去过某处或经历过某事。大部分工作。
  • Load XML via AJAX. This is a legacy system using XML in a hidden field (oh yah, baby!) between postbacks to store a wizard data structure. Rewriting it would suck.
  • 通过AJAX加载XML。这是一个遗留系统,在回发之间的隐藏字段(噢,宝贝!)中使用XML来存储向导数据结构。重写它会吸。

What I am trying to do:

我想做的是:

  • Manipulate the XML document using jQuery in IE
  • 在IE中使用jQuery操作XML文档
  • Use the same code across all browsers, using the native jQuery functionality
  • 使用本机jQuery功能,在所有浏览器中使用相同的代码

What I would be okay with:

我能接受的是:

  • Overriding/overloading the same jquery methods to get them to work in IE when manipulating the XML DOM.
  • 覆盖/重载相同的jquery方法,使它们在操作XML DOM时在IE中工作。

It just doesn't work and I feel like it just isn't possible in a 100% cross browser way using plain old jQuery methods.

它就是不管用,我觉得用100%跨浏览器的方式使用简单的旧jQuery方法是不可能的。

Case in point:

例子:

<!DOCTYPE html>
<html>
<head>
    <title>IE Sucks</title>
    <script src="Scripts/jquery-1.5.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var xml =
            '<Browsers>' +
                '<CoolBrowsers>' +
                    '<Browser name="Opera"></Browser>' +
                    '<Browser name="Chrome"></Browser>' +
                    '<Browser name="Firefox"></Browser>' +
                '</CoolBrowsers>' +
                '<BadBrowsers>' +
                    '<Browser name="IE6"></Browser>' +
                '</BadBrowsers>' +
            '</Browsers>';

        $(function () {

            $("#xml").text(xml);

            var uncoolBrowser = $("<Browser />").attr("name", "IE7");

            // In 1.5, using this...
            var $xml = $($.parseXML(xml));

            // Nope. Works everywhere else, though!
            // var $xml = $(xml);     


            // Throws a "Type mismatch"
            // Works everywhere except IE
            // This is case sensitive (??? WTF ???)
            // Lowercase "badbrowsers" nothing happens
            // Uppercase "BADBROWSERS" nothing happens
            // Best part? $xml.find("BadBrowsers").length === 1
            $xml.find("BadBrowsers").append(uncoolBrowser);

            // Only way to output XML in IE
            $("#result").text($xml[0].xml);

            // Fuggetaboutit
            // Technically, it does work in IE but not when using $.parseXML()
            // $("#result").text($("<div></div>").append($xml.clone()).html());
        });
    </script>
</head>
<body>
    <pre id="xml"></pre>
    <pre id="result"></pre>
</body>
</html>

Is it possible? Can this simple scenario be done or has IE just forsaken us all? $(xml).everything, etc. works in FF, Opera, Chrome, and Safari.

是可能的吗?这个简单的场景是可以完成的,还是IE已经把我们都抛弃了?美元(xml)。在FF、Opera、Chrome和Safari中都可以使用。

Update

更新

It is possible using voodoo magicks.

使用巫术是可能的。

I've created a jQuery plugin that takes care of reconciling the differences between different browser handling of XML. I also made an .xml() function based on similar code elsewhere, though mine fixes an IE-only issue. This works in all browsers, IE7 & IE8 for sure, can't test IE6.

我创建了一个jQuery插件,它负责协调不同浏览器处理XML之间的差异。我还基于其他地方的类似代码做了一个.xml()函数,不过我的代码修复了一个仅包含ie的问题。这在所有的浏览器中都适用,IE7和IE8当然不能测试IE6。

I have posted this on my github. If anyone has suggestions or improvements, let me know. There are several things I've already run into but I have been fixing them as I run into them.

我把这个贴在我的github上。如果有人提出建议或改进,请告诉我。有几件事我已经碰到过了,但在我碰到它们的时候,我一直在修补它们。

1 个解决方案

#1


0  

This is more of a guess as I don't know offhand what .parseXml does but IE needs createElement for unknown node names. Can you try document.createElement('BadBrowsers') for every new node you are going to manipulate?

这更像是一个猜测,因为我不知道。parsexml做什么,但是IE需要createElement来命名未知的节点名。您是否可以尝试使用document.createElement(' badbrowser ')来处理将要操作的每个新节点?

This is the case with HTML5 and that's why there are shiv scripts. You can try just taking this:

这就是HTML5的情况,这就是为什么有shiv脚本。你可以试试这个:

http://html5shiv.googlecode.com/svn/trunk/html5.js

http://html5shiv.googlecode.com/svn/trunk/html5.js

Copying it, append your new node names to var z, and then:

复制它,将新的节点名附加到var z,然后:

<!--[if lt IE 9]>
<script src="file.js"></script>
<![endif]-->

#1


0  

This is more of a guess as I don't know offhand what .parseXml does but IE needs createElement for unknown node names. Can you try document.createElement('BadBrowsers') for every new node you are going to manipulate?

这更像是一个猜测,因为我不知道。parsexml做什么,但是IE需要createElement来命名未知的节点名。您是否可以尝试使用document.createElement(' badbrowser ')来处理将要操作的每个新节点?

This is the case with HTML5 and that's why there are shiv scripts. You can try just taking this:

这就是HTML5的情况,这就是为什么有shiv脚本。你可以试试这个:

http://html5shiv.googlecode.com/svn/trunk/html5.js

http://html5shiv.googlecode.com/svn/trunk/html5.js

Copying it, append your new node names to var z, and then:

复制它,将新的节点名附加到var z,然后:

<!--[if lt IE 9]>
<script src="file.js"></script>
<![endif]-->