How come there is a difference between Chrome and Firefox here?

寵の児 提交于 2019-12-04 11:18:01

The easiest solution to this is checking for .cloneNode method and using that if it exists.

This means that your cloning method will check for any DOM nodes and use the pre defined DOM method on it. This should avoid your issue completely.

As for your actual problem. It seems Chrome and Firefox disagree on what belongs on the prototype and what belongs on the object for HTMLTableRowElement (And any other element aswell).

Compare console.dir(HTMLTableRowElement) in both firefox and chrome.

In firefox all those properties live on the HTMLTableRowElement prototype. Where as the chrome prototype only has a few methods on it. (delecteCell and insertCell).

No where in the DOM specification does it say whether propertys of HTMLElements should be defined on the prototype or on the object specifically so this is just something you should not rely on.

Either way use .cloneNode because it's a native method and therefore better/faster then anything you can write in JavaScript.

Chrome psuedo implementation:

function HTMLTableRowElement() {
    ...
    this.nextSibling = ...;
    this.nodeName = ...;
    this.nodeType = ...;
    ...
}

HTMLTableRowElement.prototype.deleteCell = function() { ... };
HTMLTableRowElement.prototype.insertCell = function() { ... };

Firefox pseudo implementation

function HTMLTableRowElement() {
    ...
}

HTMLTableRowElement.prototype.nextSibling = ...;
HTMLTableRowElement.prototype.nodeName = ...;
HTMLTableRowElement.prototype.nodeType = ...;
...
HTMLTableRowElement.prototype.deleteCell = function() { ... };
HTMLTableRowElement.prototype.insertCell = function() { ... };

I think @Raynos offers a good solution in his answer. As to why things are so different, I suspect the basic issue is that a DOM element is not a JavaScript object — that is, it does not inherit from the JavaScript "Object" class in any way. DOM elements are provided by the runtime, and have behaviors and semantics that (usually) make sense to JavaScript code, but they're not really JavaScript objects internally. Thus, to me it's somewhat amazing the "hasOwnProperty" is available to be called at all.

The easiest way to determine whether or not an object is a DOM object would be to check if that object has the nodeName, nodeValue or nodeType properties.

if ( tr.nodeType > 0 ) {
    // tr is a DOM object
} else {
    // tr is not a DOM object
}

All DOM objects implement the Node interface and therefore contain the above mentioned properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!