简体   繁体   English

为什么我需要使用Mootools Element方法扩展document.body $(document.body)?

[英]Why do I need to do $(document.body) to extend document.body with Mootools Element methods?

So after attempting to get my app working on the latest IE, it turns out IE doesn't like the following code: 因此,在尝试让我的应用程序在最新的IE上工作之后,事实证明IE不喜欢以下代码:

document.body.getElement('.className');

Firefox and Chrome responded okay, but document.body on IE had none of the Mootools Element methods. Firefox和Chrome的反应还不错,但IE上的document.body没有Mootools Element方法。

After looking at the documentation , some of the examples wrapped document.body in $() to expose it to the Mootools methods. 在查看文档之后 ,一些示例将$() document.body包装起来,以将其公开给Mootools方法。

Just wanted to know the reason why it works fine in FireFox/Chrome, but not automatically in IE? 只是想知道它在FireFox / Chrome中工作正常的原因,但不能在IE中自动运行?

This is because of the way IE exposes (or, er, does not) the Element prototype for extending. 这是因为IE暴露(或者,呃,不)元素原型的扩展方式。 In proper browsers, document.body - and everything else that is a part of the DOM and is derived from Element - inherits methods attached to the Element.prototype 在适当的浏览器中, document.body - 以及作为DOM的一部分并且从Element派生的所有其他内容 - 继承附加到Element.prototype

In old IE, this does not happen (it inherits from the built-in proto but it's read only-ish). 在旧的IE中,这不会发生(它继承自内置的原型,但它只读 - 是的)。 See any topic on why - eg. 查看有关原因的任何主题 - 例如。 Is there really no way to expose the prototype of a html element in IE (<8)? 真的没有办法在IE中公开html元素的原型(<8)吗?

In short, it's DOM. 简而言之,它是DOM。 it's not ECMA spec. 这不是ECMA规范。 they did not do it. 他们没有这样做。 they do so now (fully since IE9) 他们现在这样做(完全自IE9以来)

MooTools - being prototypical - gets around that by extending elements that it accesses manually. MooTools - 是典型的 - 通过扩展它手动访问的元素来解决这个问题。 It does so via the $ or the element constructor or Slick (when it first encounters an element). 它通过$或元素构造函数或Slick(当它第一次遇到元素时)这样做。

In IE, the extend will not only setup element storage/uid, it will copy a reference to expando properties from the Element.prototype onto the element object itself. 在IE中,扩展将不仅设置元素存储/ uid,它将从Element.prototype复制对expando属性的引用到元素对象本身。

so, if you did: 所以,如果你这样做了:

$(document.body);
document.body.addClass('bar').adopt(new Element('div')); 

this will work. 这会奏效。 You only need to extend it once, then all methods are copied onto the actual object. 您只需要扩展一次,然后将所有方法复制到实际对象上。

in the future, mootools will not be prototypical but wrapped (like jquery) so any element access will be through a $ type function. 在未来,mootools将不是原型而是包装(如jquery),因此任何元素访问都将通过$ type函数。

see https://github.com/mootools/mootools-core/blob/master/Source/Element/Element.js#L268-275 请参阅https://github.com/mootools/mootools-core/blob/master/Source/Element/Element.js#L268-275

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

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