为什么我需要使用Mootools Element方法扩展document.body $(document.body)? [英] Why do I need to do $(document.body) to extend document.body with Mootools Element methods?

查看:100
本文介绍了为什么我需要使用Mootools Element方法扩展document.body $(document.body)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在尝试让我的应用程序在最新的IE上运行之后,事实证明IE不喜欢以下代码:

So after attempting to get my app working on the latest IE, it turns out IE doesn't like the following code:

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

Firefox和Chrome回答还可以,但 document.body 在IE上没有Mootools元素方法。

Firefox and Chrome responded okay, but document.body on IE had none of the Mootools Element methods.

查看文档,一些示例包含 document.body in $()将它暴露给Mootools方法。

After looking at the documentation, some of the examples wrapped document.body in $() to expose it to the Mootools methods.

只是想知道它在FireFox / Chrome中工作正常的原因,但不能在IE中自动生成?

Just wanted to know the reason why it works fine in FireFox/Chrome, but not automatically in IE?

推荐答案

这是因为IE公开(或者,呃,没有)Element原型进行扩展的方式。在适当的浏览器中, document.body - 以及作为DOM一部分并从Element派生的所有其他内容 - 继承附加到元素的方法。原型

This is because of the way IE exposes (or, er, does not) the Element prototype for extending. 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

在旧的IE中,这不会发生(它继承自内置原型,但它只读 - 是的)。查看有关原因的任何主题 - 例如。 是否真的没有办法在IE中公开html元素的原型(< 8)了?

In old IE, this does not happen (it inherits from the built-in proto but it's read only-ish). See any topic on why - eg. Is there really no way to expose the prototype of a html element in IE (<8)?

简而言之,它是DOM。这不是ECMA规范。他们没有这样做。他们现在就这样做了(完全是因为IE9)

In short, it's DOM. it's not ECMA spec. they did not do it. they do so now (fully since IE9)

MooTools - 原型 - 通过扩展手动访问的元素来解决这个问题。它通过$或元素构造函数或Slick(当它第一次遇到元素时)这样做。

MooTools - being prototypical - gets around that by extending elements that it accesses manually. It does so via the $ or the element constructor or Slick (when it first encounters an element).

在IE中,扩展不仅会设置元素存储/ uid,它会将对Element.prototype的expando属性的引用复制到元素对象本身。

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.

所以,如果你这样做:

$(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.

将来,mootools将不是原型而是包装(如jquery)所以任何元素访问都将通过$ type函数。

in the future, mootools will not be prototypical but wrapped (like jquery) so any element access will be through a $ type function.

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

这篇关于为什么我需要使用Mootools Element方法扩展document.body $(document.body)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆