【问题标题】:jQuery 1.4.1 and Firefox 16.0.1 strange attribute issuejQuery 1.4.1 和 Firefox 16.0.1 奇怪的属性问题
【发布时间】:2012-10-05 12:18:41
【问题描述】:

当涉及到 Firefox 16.0.1 时,我最近在 jQuery 1.4.1 中遇到了一个非常奇怪且非常糟糕的错误。其他所有浏览器都很好。旧版本的 Firefox 很好,新版本的 jQuery 很好。

我有一个带有一些复选框的表格,看起来像这样:

<table class="EntityTable">
<tbody>
    <tr>
        <td>
            <input type="checkbox" class="IdCheckBox" id="Checkfor654100" name="Checkfor654100" itemId="654100" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="IdCheckBox" id="Checkfor654101" name="Checkfor654101" itemId="654101" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="IdCheckBox" id="Checkfor654102" name="Checkfor654102" itemId="654102" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="IdCheckBox" id="Checkfor654103" name="Checkfor654103" itemId="654103" />
        </td>
    </tr>
</tbody>
</table>

在 javascript/jquery 中我循环并收集所有 itemIds

    var ids = new Array();

    $('input.IdCheckBox:checked').each(function(){
       var thisBox = $(this);
       ids.push(thisBox.attr('itemId'));

    });

在 Firefox 16.0.1 中,ids 由页面的 url 填充。 /theId 喜欢:http://blahblahblah.com/654101

我只需将其更改为即可解决此问题:

ids.push(thisBox.attr('itemid'));

但是,我想知道为什么会发生这种情况,以及是否有其他任何事情受此影响。

这是一个 JS Fiddle,它展示了这个问题的全部荣耀: http://jsfiddle.net/K8jRf/8/

谢谢!

【问题讨论】:

  • 解决方案:不要使用旧版本的jQuery....
  • 我有一个解决方案,我发布了。它并不总是像“不要使用旧版本”那么简单,我维护一个可能有 5000 多行 javascript/jquery 的应用程序。并且有很多重大更改只是一时兴起更新到新版本的 jquery。
  • 较新的版本很可能不会破坏您的代码,除非它本身存在问题。
  • 它会,整个 .attr 与 .prop 的事情,并且 ajax 的工作方式发生了一些变化。
  • attr 与 prop 的问题在 1.6.1 中大部分已修复。前端的 ajax 在大多数情况下仍然可以正常工作。唯一主要改变的是后端代码。

标签: jquery attributes jquery-1.4 firefox-16


【解决方案1】:

这似乎不是 jQuery 问题/错误,而是 FF16(和 FF17b)javascript 问题/错误。 以下 html-sn-p 显示了 itemId 在(至少)li 元素上的特殊行为:

<html><body><ul><li id="li1"></li></ul><script>
var element = document.getElementById("li1");
// assign to bla
element.bla = "hello";
// after
alert( element.bla ); // ok -> hello
// assign
element.itemId = "hello";
// after
alert( element.itemId ); // nok -> <file uri>hello
</script>
</body>
</html>

从 FF16 开始,“itemId”似乎已成为“特殊属性/行为”。 typeof(element.itemId) 是“字符串”,甚至在赋值之前... FF15 的行为符合预期。

【讨论】:

  • 非常有趣。我想知道为什么当我将 jQuery 的版本更改为更新的版本时它在我的小提琴中起作用!
  • 看起来它的功能不是错误... ;-) 见 bugzilla
  • 是的,我一直在看那个错误......非常有趣的“功能”
  • 看起来它是 HTML5 规范的一部分 whatwg.org/specs/web-apps/current-work/multipage/…
【解决方案2】:

Firefox 将属性名称“规范化”为全部小写。

如果您在使用attr 之前使用data,您可以这样做:

<input type="checkbox" class="IdCheckBox"
                id="Checkfor654101" name="Checkfor654101" data-itemId="654101" />

Js:

var itemId = $("input").data("itemId");

【讨论】:

  • 有趣,将 itemId 作为属性或字符串有什么特别之处吗?因为我拥有的其他属性,即驼峰式,很好!
  • @Patricia 很奇怪,我不确定。
猜你喜欢
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 2011-06-23
相关资源
最近更新 更多