Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
e、 Javascript原型模块模式中的preventDefault()_Javascript_Jquery_Module_Prototype - Fatal编程技术网

e、 Javascript原型模块模式中的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

我试图添加e.preventDefault();当单击一个标记时。下面是我的代码,但它不工作。谁能给我一个建议吗?多谢各位

(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加载。