gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:35:48 24 4
gpt4 key购买 nike

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

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

Firefox 和 Chrome 响应正常,但 IE 上的 document.body 没有任何 Mootools Element 方法。

看了the documentation之后, 一些示例将 document.body 包装在 $() 中以将其暴露给 Mootools 方法。

只是想知道为什么它在 FireFox/Chrome 中运行良好,但在 IE 中不能自动运行?

最佳答案

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

的方法

在旧的 IE 中,这不会发生(它继承自内置原型(prototype),但它是只读的)。查看有关原因的任何主题 - 例如。 Is there really no way to expose the prototype of a html element in IE (<8)?

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

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

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

所以,如果你这样做了:

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

这会起作用。您只需要扩展它一次,然后所有方法都被复制到实际对象上。

在未来,mootools 将不再是原型(prototype)而是包装(如 jquery),因此任何元素访问都将通过 $ 类型函数。

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

关于javascript - 为什么我需要执行 $(document.body) 以使用 Mootools Element 方法扩展 document.body?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13075790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com