我有这段代码:
var beer = function(){};
$.extend(beer.prototype, {
types:{
'.beer':'boom',
'.beer_pick':'pack',
'.beer_tes':'fix',
},
init:function() {
var me = this;
$.each(this.types, function(key, value) {
me.update(key, value);
});
},
update:function(className, actionName) {
$(className + ' .bb').click(function() {
$(this).parent().find('.cc').load('try.php?do=beer&action=' + actionName);
alert("TEST");
});
},
pics:{
add:function(element) {
$.post("try.php?do=beer&action=pic", {
id:$(element).data('id'),
name:$(element).data('name'),
});
}
}
});
beer = beer.prototype;
beer.init();
在函数update
中,点击事件无效,
点击元素后,没有任何反应。
当我将click事件拉出函数时,如下所示:
$(document).ready(function() {
className = '.beer';
actionName = 'boom';
$(className + ' .bb').click(function() {
$(this).parent().find('.cc').load('try.php?do=beer&action=' + actionName);
alert("TEST");
});
});
有效。 为什么?我该如何解决这个问题?
另一个问题: 有更好的方法:
beer = beer.prototype;
beer.init();