.each()在IE8中不起作用

时间:2013-12-11 17:07:33

标签: jquery xml

我从XML中提取信息并将其放入我使用jquery each()和append()函数构建的几个div中。

除IE8外,它适用于所有浏览器。在IE8中,该功能似乎根本没用。

有谁知道这是为什么?

var xml = '<questions><question1 text="When was the first solar photovoltaic cell    invented?"><choice>1940s</choice><choice answer="correct">1950s</choice><choice>1970s</choice><choice>1980s</choice><correct>Thats right</correct><wrong>It was in the 50s</wrong><correctanswer>The first modern solar cell was created by ATT Bell in 1954. One of the first uses for the technology was on early space satellites like the Vanguard 1.</correctanswer><wronganswer>The first modern solar cell was created by ATT Bell in 1954. One of the first uses for the technology was on early space satellites like the Vanguard 1.</wronganswer></question1></questions>';

xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
currentQues = 1
curr = 'question' + currentQues;
$question = $xml.find(curr);

$question.find("choice").each(function(){
            x = $(this).attr("answer");
            ctext = $(this).text();
            $(".answers").append("<div class='ans'><input id='" + x + "' type='radio'><label>" + ctext + "</label></div>");
        });

1 个答案:

答案 0 :(得分:1)

试试这个:Fiddle

  $(document).ready(function(){

      var xml = '<questions><question1 text="When was the first solar photovoltaic cell    invented?"><choice>1940s</choice><choice answer="correct">1950s</choice><choice>1970s</choice><choice>1980s</choice><correct>Thats right</correct><wrong>It was in the 50s</wrong><correctanswer>The first modern solar cell was created by ATT Bell in 1954. One of the first uses for the technology was on early space satellites like the Vanguard 1.</correctanswer><wronganswer>The first modern solar cell was created by ATT Bell in 1954. One of the first uses for the technology was on early space satellites like the Vanguard 1.</wronganswer></question1></questions>';

      var appendtext = "";

      xmlDoc = $.parseXML( xml ),
      $xml = $( xmlDoc ),
      currentQues = 1
      curr = 'question' + currentQues;
      $question = $xml.find(curr);


    $question.find("choice").each(function(){
        x = $(this).attr("answer");

        if (typeof x !== 'undefined' && x !== false) {

                 ctext = $(this).text();
                 appendtext =  "<div class='ans'><input id='" + x + "' type='radio'><label>" + ctext + "</label></div>";

        }
    });
    $( ".answers" ).append(appendtext );
});

说明:您必须检查x是否未定义。检查具有<choice> attr的answer标记的含义 - 否则不添加附加文本。

另外,我在IE8兼容版本上进行了测试 - 工作正常。