14

I'm using:

<span id="nodeName"></span>

in my html, then let jquery do:

$("#nodeName").html("someString");

Then, the console says that:

Uncaught TypeError: Object #<HTMLSpanElement> has no method 'toLowerCase' 

After I change the id, it works all right. So, is there any reserved id?

4

2 回答 2

16

No, almost any string is a valid ID. And this only happens when you include jQuery (a 1.xx version, not a 2.xx one).

More details :

  • the code works, as in "it changes the innerHTML"
  • as soon as you include jQuery, even if you don't use it, you have an error in acceptData, when the dom is ready, but after the callback you passed to $(document).ready was executed.

It's a jQuery bug. Why it happens seems to be due to window.nodeName being defined to this element (which doesn't have a toLowerCase method as it's an element) by the fact the element with this id exists. It seems to be OK in jQuery 2.02.

于 2013-06-26T19:43:45.023 回答
10

它在以下功能中失败:

acceptData: function( elem ) {
    // Do not set data on non-element because it will not be cleared (#8335).
    if ( elem.nodeType && elem.nodeType !== 1 && elem.nodeType !== 9 ) {
        return false;
    }

    var noData = elem.nodeName && jQuery.noData[ elem.nodeName.toLowerCase() ];

    // nodes accept data unless otherwise specified; rejection can be conditional
    return !noData || noData !== true && elem.getAttribute("classid") === noData;
}

具体就叫elem.nodeName.toLowerCase()什么时候elem === window。当您在页面中包含 jQuery 时会调用它,即使您从未在 Javascript 中选择该元素。

原因是 jQuery 会检查data-attributes一旦 jQuery 准备好后哪些元素可以处理。在该检查期间,它调用window元素上的 acceptData 函数。

这在 jQuery 1 的最新版本中,从版本 1.8.0 到并包括最新的 1.10.1。该错误似乎是由 jQuery 1.8 中的以下更改引入的:

$(element).data(“events”):在 1.6 版本中,jQuery 将其内部数据与用户数据分开以防止名称冲突。但是,有些人正在使用内部未记录的“事件”数据结构,因此我们仍然可以通过 .data() 检索它。这现在在 1.8 中被删除,但您仍然可以通过 $._data(element, "events") 获取事件数据以进行调试。请注意,这不是受支持的公共接口;实际数据结构可能因版本而异。

窗口在版本 1.8.0的第 2939 行被传递,jQuery._data以便检查窗口对象上的内部“事件”数据。这发生在 jQuery 触发事件时。由于 window 不是 DOM 节点 acceptData 根本不应该在 window 上调用。cur$(document).ready $(window).ready

handle = ( jQuery._data( cur, "events" ) || {} )[ event.type ] && jQuery._data( cur, "handle" );

我创建了一个错误报告

于 2013-06-26T20:01:16.517 回答