我創建在Mootools的一個新元素,其具有的事件,這樣的:在MooTools的:在元素構造活動
var div = new Element('div', {
id: 'dynamic',
'class': 'injected',
styles: {
color: '#f55'
},
html: 'Hong Kong Phooey, number one super guy. Hong Kong Phooey, quicker than the human eye. He\'s got style, a groovy style, and a car that just won\'t stop. When the going gets tough, he\'s really rough, with a Hong Kong Phooey chop (Hi-Ya!). Hong Kong Phooey, number one super guy. Kong Phooey, number one super guy. Hong Kong Phooey, quicker than the human eye. He\'s got style, a groovy style, and a car that just won\'t stop. When the going gets tough, he\'s really rough, with a Hong Kong Phooey chop (Hi-Ya!). Hong Kong Phooey, number one super guy.',
events: {
click: function(event) {
alert('clicked');
},
mouseenter: function(event) {
var self = $('dynamic');
self.setStyle('color', '#090');
},
mouseleave: function(event) {
var self = $('dynamic');
self.setStyle('color', '#f55');
}
}
});
div.inject(document.body);
它是一個糟糕的技術來獲得具有自我股利= $(「動態」)每個事件?我試過
mouseenter: function(event) {
this.setStyle('color', '#090');
}.bind(this)
認爲「this」會引用我正在構建的元素。但相反,它指的是全球窗口。
我是否正確的做事?
謝謝!
非常感謝Dimitar的解釋! – 2011-04-08 13:08:56