gpt4 book ai didi

javascript - Mootools:元素构造函数中的事件

转载 作者:行者123 更新时间:2023-11-30 10:50:36 24 4
gpt4 key购买 nike

我正在 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);

在每个事件中使用 self = $('dynamic') 获取 div 是一种糟糕的技术吗?我试过了

mouseenter: function(event) {
this.setStyle('color', '#090');
}.bind(this)

认为“this”指的是我正在构建的元素。但它指的是全局窗口。

我做事的方式是否正确?

谢谢!

最佳答案

你可以使用event.target

events: {
mouseenter: function(event) {
event.target.setStyle('color', '#090');
},
mouseleave: function(event) {
event.target.setStyle('color', '#f55');
}
}

关于javascript - Mootools:元素构造函数中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5595203/

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