IE - JQuery在$ .each循环中将子节点附加到XML

时间:2015-04-23 19:10:34

标签: javascript jquery xml internet-explorer

我有一个问题,IE没有正确附加或识别我尝试将子节点附加到解析的XML字符串中。 Chrome和Firefox了解我尝试做的事情。我不知道这是范围问题还是IE和XML的行为。

var dataContainers = $('[data-container]');
var containerXML = $.parseXML('<inlineSubmission><userid>'+userID+'</userid><guid>'+GUID+'</guid><contentField><![CDATA[FlexXML]]></contentField><content><containers></containers></content></inlineSubmission>');

$.each(dataContainers,function (key,value) {
var containerID = $(value).attr('id'),
        isVisible = $(value).is(':visible'),
        xmlFragment;

xmlFragment = '<container><name>'+containerID+'</name><visible>'+isVisible+'</visible></container>';

$(containerXML).find('containers').append(xmlFragment);
});

console.log(containerXML)

Chrome和Firefox都正确返回:

<inlineSubmission><userid>55555</userid><guid>22222-222-2-22-222</guid><contentField><![CDATA[FlexXML]]></contentField><content><containers><container><name>heroContainer</name><visible>true</visible><bgcolor>undefined</bgcolor><textcolor>undefined</textcolor><subitem><name>contactInfo</name><visible>none</visible><location>undefined</location></subitem></container><container><name>contentContainer</name><visible>true</visible></container><container><name>cnmContainer</name><visible>true</visible></container><container><name>accountAccessContainer</name><visible>true</visible></container><container><name>promoContainer</name><visible>true</visible></container><container><name>contactContainer</name><visible>true</visible></container></containers></content></inlineSubmission>

当IE返回原始变量值时:

<inlineSubmission><userid>55555</userid><guid>22222-222-2-22-222</guid><contentField><![CDATA[FlexXML]]></contentField><content><containers /></content></inlineSubmission>

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

尝试将$serverRoot = "xxx.xxx.xxx." $startVal = 100 $stopVal = 150 # First start with creating a collection $servers = @() for ($i = $startVal; $i -le $stopVal; $i++) { $servers += ($serverRoot + $i) } # Then, pass the $servers variable directly when creating the session $session = New-SSHSession -ComputerName $servers -Credential $credential -AcceptKey 替换为$(containerXML.documentElement)

$(containerXML)返回$(containerXML)#document context,例如;

document

不会将$(document).append("<p>abc</p>")追加到<p>abc</p><html>个元素,并且可能会返回错误:

<body>

TypeError: Cannot read property 'createDocumentFragment' of null返回$(containerXML.documentElement)元素,<inlinesubmission> context

inlinesubmission
var userID = "abc", GUID = 123, containerID = 456, isVisible = true;

var xmlFragment = '<container><name>'
                  +containerID+'</name><visible>'
                  +isVisible+'</visible></container>';

var containerXML = $.parseXML('<inlineSubmission><userid>'
                              +userID+'</userid><guid>'
                              +GUID+'</guid><contentField><![CDATA[FlexXML]]>'
                              +'</contentField><content><containers></containers>'
                              +'</content></inlineSubmission>');

console.log($(containerXML)
            , $(containerXML.documentElement).find("containers"));

$(containerXML.documentElement).find("containers").append(xmlFragment);

$("body").append($(containerXML.documentElement));

答案 1 :(得分:1)

终于搞定了。谢谢你的回答!他们给了我正确的入住方向:

var containerXML = $('<inlineSubmission><userid>'+userID+'</userid><guid>'+GUID+'</guid><contentField>FlexXML</contentField><content><containers></containers></content></inlineSubmission>');

$.each(dataContainers,function (key,value) {
var containerID = $(value).attr('id'),
    isVisible = $(value).is(':visible'),
    xmlFragment;

xmlFragment = $('<container><name>'+containerID+'</name><visible>'+isVisible+'</visible></container>')

$(containerXML).find('containers').append(xmlFragment);
});

var parsedXML = $.parseXML(containerXML[0].outerHTML);
console.log(parsedXML)