gpt4 book ai didi

javascript - 点击后如何调用对象函数

转载 作者:行者123 更新时间:2023-11-28 20:24:02 25 4
gpt4 key购买 nike

我想知道如何使这段代码工作:http://jsfiddle.net/LBXVd/2/ 。它几乎是干净的jqueryboilerplate,我对这部分代码感兴趣:

Plugin.prototype = {

init: function() {

/// create element <a> with click function
myEl = $('<a/>', {
href: '#',
text: '###',
click: function(e){this._handler();}
});
$('ul').before(myEl);
},


//handler for click events on my <a> element...
_handler: function(el, options) {
alert("my handler called");
}
};

点击创建的元素后如何调用函数_handler?

谢谢!

最佳答案

你的jsfiddle不是很有帮助,因为它没有显示任何结果......但我认为这是因为thisthis._handler() call 不是您想象的那样(它是 <a> 元素,而不是原型(prototype)对象)。

尝试

init: function() {

var self = this; // ****

/// create element <a> with click function
myEl = $('<a/>', {
href: '#',
text: '###',
click: function(e){self._handler();} // ****
});

关于javascript - 点击后如何调用对象函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17722437/

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