e、 Javascript原型模块模式中的preventDefault()
我试图添加e.preventDefault();当单击一个标记时。下面是我的代码,但它不工作。谁能给我一个建议吗?多谢各位e、 Javascript原型模块模式中的preventDefault(),javascript,jquery,module,prototype,Javascript,Jquery,Module,Prototype,我试图添加e.preventDefault();当单击一个标记时。下面是我的代码,但它不工作。谁能给我一个建议吗?多谢各位 (function($){ 'use strict'; var ItemPhoto = function(){ this.$el = undefined; this.getElements(); this.events(); } ItemPhoto.prototype = { g
(function($){
'use strict';
var ItemPhoto = function(){
this.$el = undefined;
this.getElements();
this.events();
}
ItemPhoto.prototype = {
getElements: function(){
this.$el = $('#contaner').find('.image');
this.$item = $('#contaner').find('.item').find('a');
},
events: function(){
this.$item.on('click', {_self:this}, this.swapImage());
},
swapImage: function(e){
e.preventDefault();
var _self = e.data._self;
_self.$el.attr('src', _self.$item.attr('href'));
}
}
var itemPhoto = new ItemPhoto();
})(jQuery);
看起来你的代码运行得很好。检查jquery是否已加载,函数等待DOM加载。由于
()
,您正在调用swapImage
而不是将其绑定到事件处理程序。好的,我编辑了您提到的行,但它仍然不起作用。。因此,我将以重复的方式回滚并结束。感谢Oriol回滚问题。对不起,我不太理解这里的规则。我很感激你检查了我的问题。我认为你不能复制问题中描述的问题,你应该投票(或标志,如果你没有足够的声誉)关闭它。不要随意猜测答案,谢谢。我将检查jQuery和DOM加载。