我想添加e.preventDefault();当a标签被点击。下面是我的代码,但它不起作用。有人可以给我一个建议吗?谢谢。e.preventDefault()在Javascript原型模块模式
(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);
感谢'()',您调用'swapImage'而不是绑定到事件处理函数。 – ssube
好吧,我编辑了你提到的这一行,但它仍然不起作用。 – shinyatk
[你的编辑解决了这个问题](https://jsfiddle.net/rx96ma6d/)。所以我正在回滚和关闭重复。 – Oriol