gpt4 book ai didi

javascript - 如何访问事件绑定(bind)中的对象方法

转载 作者:行者123 更新时间:2023-12-01 01:38:11 26 4
gpt4 key购买 nike

(function() {
let create = {

init() {
this.cacheDom();
this.bindEvent();
},

bindEvent() {
$(this.$content_buttons).click(function() {
create.preformAction(this);
});
},

cacheDom() {
this.$content_buttons = $('.content_button');
},

preformAction(el) {
alert('its class is ' + $(el).attr('class'));
}
};
create.init();
}());

考虑上面的代码,我可以通过执行此 create.performAction 访问 create 对象的 performAction 方法。还有其他办法吗?提前致谢。

最佳答案

尝试一下:

(function() {
let create = {

init() {
this.cacheDom();
this.bindEvent();
},

bindEvent() {

$(this.$content_buttons).click(function() {
create.preformAction(this);
});
},

cacheDom() {
this.$content_buttons = $('.content_button');
},

preformAction(el) {
alert('its class is ' + $(el).attr('class'));
}
};
create.init();
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="content_button class1">click1</button>
<button class="content_button class2">click2</button>

关于javascript - 如何访问事件绑定(bind)中的对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52639469/

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