【问题标题】:jQuery using html() on array not working in IEjQuery 在数组上使用 html() 在 IE 中不起作用
【发布时间】:2010-12-20 00:12:09
【问题描述】:

我有一些 XML

<carouselitem name='carouselitem'><flick><pic>/images/test1.jpg</pic><headertext>sdfsdfsdf csfdfsdf</headertext><dek>sdfsfsd sdfsf dsfsfdsfds sdf sdfsfds</dek></flick></carouselitem>

包装在 DIV 中,id 为 carousel。以下适用于FF

var carouselarray = $('#carousel carouselitem');
    jQuery.each(carouselarray, function(){
        var row_to_insert = $(this).html();
        carouselxml += row_to_insert;
    });

row_to_insert 变量在 FF 中被 XMl 填充,但在 IE 和 Chrome 中为空。任何帮助都将不胜感激

谢谢

【问题讨论】:

    标签: jquery html arrays


    【解决方案1】:

    我认为这是失败的,因为您正在创建自定义标签。如果您查看 innerHTML 属性,您会发现它对于 IE 是空白的。要让 IE 识别自定义标签,请查看:http://ajaxian.com/archives/adding-custom-tags-to-internet-explorer-the-official-way & http://msdn.microsoft.com/en-us/library/ms531076%28VS.85%29.aspx

    【讨论】:

    • 这很好用。定义了自定义字段并将其拉入。不幸的是,现在 IE 忽略了其中 2 个字段中的 CDATA。现在必须解决这个问题
    【解决方案2】:

    我建议不要将 XML 直接插入到您的 HTML 中,而是将其放入文件中

    carousel.xml

    <carousel>
        <carouselitem name="carouselitem">
        ...
    

    然后使用 jquery ajax 函数调用它。 IE 需要解决,因为有一些奇怪的错误,它不能正确处理来自本地计算机的传入 xml。从服务器它没有这个问题。

    $.ajax({
        url: "carousel.xml",
        dataType: ($.browser.msie) ? "text" : "xml",
        success: function(data) {
            var xml;
            if (typeof data == "string") {
                xml = new ActiveXObject("Microsoft.XMLDOM");
                xml.async = false;
                xml.loadXML(data);
            } else {
                xml = data;
            }
    
            $(xml).find("carouselitem").each(function() {
                //your code...
            });
        )
    });
    

    【讨论】:

      【解决方案3】:

      我认为您需要使用 [name=carouselitem] 来区分您要查找的属性。此代码(无论如何对我而言)正确填充 row_to_insert。让我知道这是否是您正在寻找的。​​p>

      var carouselxml = '';
      
      var carouselarray = $('#carousel [name=carouselitem]');
          jQuery.each(carouselarray, function(){
                  var row_to_insert = $(this).html();
                  console.log(row_to_insert);
                  carouselxml += row_to_insert;
          });
      

      【讨论】:

      • 感谢您的回复,但事实并非如此。 row_to_insert 在 FF 上得到了很好的填充,它只是在我遇到问题的 IE 中。将呼叫切换到 [name=carouselitem] 并不能解决此问题。
      • 我以前在这种情况下遇到过stevetucker.co.uk/page-innerxhtml.php 的运气。您可能想试一试。
      猜你喜欢
      • 1970-01-01
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 2012-07-21
      相关资源
      最近更新 更多