我正在尝试创建一个方法,该方法将从页面的另一部分复制HTML,然后为其中的元素设置单击处理程序。我遇到的问题是处理程序在单击时没有触发。
它很有趣,因为当我将处理程序设置为匿名函数时,它会在单击时触发,但是当我将其设置为self.initSizeSZ或myModule.initSizeSZ时,它将不会触发。
// Elements are cached for performance.
var $selectSizes = $('#someSelector');
var $catalogWheels = $('#someOtherSelector');
var myModule = {
// This is the module that calls renderSizeSZ
loadWheels: function(){
if(!this.loaded){
this.renderSizesSZ();
this.loaded = true;
}
},
renderSizesSZ: function(){
var self = this;
var $clone = $selectSizes.clone(false);
var html = $selectSizes.html();
$catalogWheels
.find('#icf_catalog-sizes')
.html(html)
.find('li a')
// if this is set as an anonymous function, it will fire, but as a named function it won't
.click(self.initSizeSZ);
},
initSizeSZ: function(event){
console.log('firing')
}
}