我想添加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