jquery - 如何在 Backbone.js 中添加鼠标悬停事件

标签 jquery backbone.js

M 尝试在我的 Backbone View 中提供 mouseover 事件,这是我的 View :

Backbone.View.extend({
  template :_.template( '<li class="<% if (refertype=="U"){%>info <% }else{%> access<%}%> main"><%=refername%>'+

            '</li>'),
  initialize: function() {
    _.bindAll(this, 'render', 'close');
    this.model.bind('change', this.render);
    this.model.view = this;
  },
    events: {
        "mouseover .main": "mouseovercard"
    },
  // Re-render the contents of the Card item.
  render: function() {
    this.el=this.template(this.model.toJSON());
    $(".cards-list").append(this.el);
  },
    mouseovercard: function() {
        console.log("hello world");
    }
});

但是当我将鼠标悬停在 main 类上时,它没有显示 hello world,请建议该怎么办?

尝试过 Heikki Answer 但鼠标悬停不起作用?

App.Backbone.CardView = Backbone.View.extend({
    tagName: 'li',
    className: 'main',
  initialize: function() {
    _.bindAll(this, 'render');
    this.model.bind('change', this.render);
    this.model.view = this;
  },
    events:{
        "mouseover .main": "mouseovercard"
    },
  // Re-render the contents of the Card item.
  render: function() {
         $(this.el)
            .removeClass('info access')
            .addClass(this.model.get('refertype') == 'U' ? 'info' : 'access')
            .text(this.model.get('refername'));
    $(".cards-list").append(this.el);
  },
    mouseovercard: function() {
        console.log("hello world");
    }
});

最佳答案

您正在替换事件绑定(bind)到的 View 的根元素。

试试这个:

Backbone.View.extend({

    tagName: 'li',

    className: 'main',

    events: {
        'mouseover': 'mouseovercard'
    },

    initialize: function() {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
    },

    render: function() {
        $(this.el)
            .removeClass('info access')
            .addClass(this.model.get('refertype') == 'U' ? 'info' : 'access')
            .text(this.model.get('refername'));
        return this;
    },

    mouseovercard: function() {
        console.log('hello world');
    }

});

http://documentcloud.github.com/backbone/#View-extend

关于jquery - 如何在 Backbone.js 中添加鼠标悬停事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6583029/

相关文章:

JavaScript 和 PHP 提交数据

javascript - 是否可以使用 Jquery 模拟 Ctrl+F 组合键?

javascript - 将特定图像设置为 Div

javascript - Backbone.js 在集合添加时触发渲染两次

javascript - 主干保存/获取 VS DAO?

jquery - 引导多选 : How to change backgroung color if None selected

javascript - 所有PHP页面都包含整套CSS和JS,效率高吗?

backbone.js - 是否可以为集合动态定义比较器函数?

javascript - 保存事件前后的 Backbone js

ruby-on-rails - Backbone.js 或 Ember.js 与 Ruby on Rails