我是一名中级前端 JS 开发人员,我正在尝试 Chris Coyyer在这里概述的模块模式。
但是当我在设置中存储一个 jQuery 选择器时,我无法使用它来触发点击事件。请参阅下面的代码和我的评论......非常感谢任何帮助!
var s,
TestWidget = {
settings: {
testButton: $("#testing")
},
init: function() {
s = this.settings;
this.bindUIActions();
},
bindUIActions: function() {
console.log(s.testButton); // This works: [context: document, selector: "#testing", constructor: function, init: function, selector: ""…]
//This doesn't work - why?????
s.testButton.click(function() {
//Why isn't this triggered?
alert('testButton clicked');
});
/*This works, obviously:
$('#testing').click(function() {
alert('testButton clicked');
});
*/
}
};
$(document).ready(function() {
TestWidget.init();
});