Mootools 类 - 在类属性中存储对文档正文的引用
大家好!我正在尝试学习 Mootools 课程。我创建了这个类来向页面添加一个 div。
var F = new Class({
Implements: [Options, Events],
options: {
container: document.body,
width: '250px',
background: '#ccc'
},
initialize: function(options) {
this.setOptions(options);
this.addDemoDiv();
},
addDemoDiv: function() {
var dDiv = new Element('div', {
'class': 'myClass',
html: 'Click me!',
styles: {
padding: '20px',
border: '1px solid #999',
width: this.options.width,
background: this.options.background
},
events: {
click: this.animate
}
});
dDiv.inject(this.options.container);
},
animate: function() {
alert('Hello world');
}
});
window.addEvent('domready', function() {
var item = new F();
});
它应该允许用户指定将 div 注入到的容器,文档主体是默认的。当我像上面那样做时,代码验证正常,但脚本无法添加 div - Firebug 和 Chrome 抱怨容器为 null 或未定义。
我必须将 dDiv.inject(this.options.container); 更改为此
if (!this.container) {
dDiv.inject(document.body);
} else {
dDiv.inject(this.container);
}
才能使其工作。
任何明智的 Mootools ninja 都可以告诉我为什么当我直接传递 document.body 时注入有效,但当我尝试传递对据称存储在类容器选项中的 document.body 的引用时注入会中断?我尝试过 document.body 的变体,例如 'document.body' 和 $$('document.body') 和 $$(document.body)。
Greetings all! I'm trying to learn Mootools classes. I've made this class to add a div to the page.
var F = new Class({
Implements: [Options, Events],
options: {
container: document.body,
width: '250px',
background: '#ccc'
},
initialize: function(options) {
this.setOptions(options);
this.addDemoDiv();
},
addDemoDiv: function() {
var dDiv = new Element('div', {
'class': 'myClass',
html: 'Click me!',
styles: {
padding: '20px',
border: '1px solid #999',
width: this.options.width,
background: this.options.background
},
events: {
click: this.animate
}
});
dDiv.inject(this.options.container);
},
animate: function() {
alert('Hello world');
}
});
window.addEvent('domready', function() {
var item = new F();
});
It's supposed to allow the user to specify the container to inject the div into, with the document body being the default. When I do it like above, the code validates OK, but the script fails to add the div - Firebug and Chrome complain about the container being null or undefined.
I have to change dDiv.inject(this.options.container);
to this
if (!this.container) {
dDiv.inject(document.body);
} else {
dDiv.inject(this.container);
}
to make it work.
Can any wise Mootools ninja tell me why inject works when I pass document.body in directly, but breaks when I try to pass it a reference to document.body supposedly stored in the class's container option? I've tried variations on document.body, like 'document.body' and $$('document.body') and $$(document.body).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,当您的类代码被解释时,
document.body
不可用,这通常在您的脚本放置在标记中时发生。将脚本移动到文档底部(就在
之前)可以解决很多问题,并且是一种很好的做法,因为您的脚本也不会再阻止 HTML 渲染。
最好避免在类中放置静态默认 DOM 引用,因为它们的可用性总是值得怀疑的。您可以将
options.container
保留为 null 并将方法更改为:因此,如果
this.options.container
未设置(假),它将默认为document.body
,这样,如果您确实愿意,也可以将脚本保留在中。
My guess is that
document.body
is not available when your class code gets interpreted, this usually occurs when your script is placed in the<head>
tags. Moving your script(s) to the bottom of the document (just before</body>
) solves a lot and is good practise since your script(s) won't block HTML rendering anymore either.It's also better to avoid putting a static default DOM references in your class as their availability is always questionable. You can keep
options.container
null and change your method to:So if
this.options.container
is not set (falsy) it will default todocument.body
, this way you can also keep your script(s) in the<head>
if you really want to.