简体   繁体   English

jQuery在Internet Explorer中不起作用

[英]jQuery not working in Internet Explorer

I am trying to create a dynamic menu by reading an XML file using jQuery. 我试图通过使用jQuery读取XML文件来创建动态菜单。 I have developed the code, and it works fine in Firefox 3 and Chrome, however it just doesn't work for Internet Explorer 7/8. 我已经开发了代码,并且可以在Firefox 3和Chrome中正常工作,但是不适用于Internet Explorer 7/8。

I'm posting my code below. 我在下面发布我的代码。 What is the matter with it? 怎么了

  var menu ="";
  $(document).ready(function()
  {
      $.ajax({
          type: "GET",
          url: "menu.xml",
          dataType: "xml",
          success: parseXml
      });
  });

  function parseXml(xml)
  {
      $(xml).find('link').each(function(x){
          var link = $(this);
          var title = link.attr("name");

          menu += "<div class='AccordionPanel AccordionPanelClosed'>";
          menu += "<div class='AccordionPanelTab'><span></span>";
          menu += "<a href='javascript:;'>"+title+"</a></div>";

          link.find("inLink").each(function(z){
              var intitle = $(this).attr("name");
              menu += "<div class='AccordionPanelContent'>";
              menu += "<ul><li>";
              menu += "<a href='"+$(this).attr("ref")+"'>"+intitle+"</a>";
              menu += "</li></ul></div>";
          });
          menu += "</div>";
      });

      $("#LeftMenu").append(menu);
  }

The XML file has the following structure XML文件具有以下结构

  <links>
      <link name="Reception" ref="index.html">
          <inLink name="Registration" ref="registration.html"/>
          <inLink name="Inquiry" ref="#"/>
      </link>
      <link name="Records" ref="#">
          <inLink name="Records" ref="#"/>
          <inLink name="Records2" ref="#"/>
      </link>
  </links>

I had a similar problem parsing an XML AJAX return, it worked fine on FF, but failed on IE. 我在解析XML AJAX返回时遇到了类似的问题,它在FF上工作正常,但在IE上失败。

The problem I had was extra nodes between the nodes that you are expecting. 我遇到的问题是您期望的节点之间有多余的节点。 IE adds text nodes with whitespace to the XML DOM where there is whitespace in the XML file. IE将带有空格的文本节点添加到XML DOM中,其中XML文件中存在空格。

I fixed it by changing the generated XML so there was no whitespace between nodes. 我通过更改生成的XML进行了修复,因此节点之间没有空格。

Could you try: 您可以尝试:

 $("#LeftMenu").append($(menu));

it's just a thought though. 这只是一个想法。

I could be wrong, but try this for your ajax request instead. 我可能是错的,但是请尝试将其用于您的ajax请求。

$.ajax({ type: "GET", url: "menu.xml", dataType: "xml", success: function(xml){parseXml(xml);} }); $ .ajax({类型:“ GET”,网址:“ menu.xml”,数据类型:“ xml”,成功:function(xml){parseXml(xml);}}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM