gpt4 book ai didi

javascript - 了解 Backbone.js 事件处理程序

转载 作者:搜寻专家 更新时间:2023-11-01 04:47:45 27 4
gpt4 key购买 nike

这是我的观点:

$(function() {
var ImageManipulation = Backbone.View.extend({

el: $('body'),
tagName: "img",

events: {
'mouseover img': 'fullsize',
'click img#current': 'shrink'
},
initialize: function() {
_.bindAll(this, 'render', 'fullsize', 'shrink');

//var message = this.fullsize;
//message.bind("test", this.fullsize);
},
render: function() {

},
fullsize: function() {
console.log("in fullsize function");
console.log(this.el);
$('.drop-shadow').click(function() {
console.log(this.id);
if (this.id != 'current') {
$('.individual').fadeIn();
$(this).css('position', 'absolute');
$(this).css('z-index', '999');
$(this).animate({
top: '10px',
height: '432px',
}, 500, function() {
this.id = "current";
console.log("animation complete");
return true;
});
};
});
},
shrink: function() {
$('.individual').fadeOut();
$('#current').animate({
height: '150px',
}, 500, function() {
this.id = "";
$(this).css('position', 'relative');
$(this).css('z-index', '1');
console.log("animation complete");
return true;
});
}
});
var startImages = new ImageManipulation();
});

我不明白的是如何更改 el 以使“this”接管我的全尺寸点击功能。我更愿意删除 click jQuery 函数,让鼠标悬停函数成为另一次单击,但我似乎无法弄清楚如何将“this”分配给被单击的特定图像。我希望我的问题是有道理的。

最佳答案

Backbone 的事件处理程序假设您想了解每个事件的对象(包括它的代码和它的 DOM 表示,View.el 对象),并且该事件旨在改变 View 和/或模型的某些方面。点击的实际目标是假定您知道或假定能够推导出的内容。

推导相当简单:

fullsize: function(ev) {
target = $(ev.currentTarget);

并在对 target. 的调用中替换所有对 this. 的引用。 this. 将继续引用 View 实例。在您的内部函数中,分配给 .drop-shadow 的匿名函数 this. 将引用刚刚单击的对象。如果您想访问周围的上下文,请使用闭包转发习惯用法:

fullsize: function(ev) {
var target = ev.currentTarget;
var self = this;
$('.drop-shadow').click(function(inner_ev) {
console.log(this.id); // the same as inner_ev.currentTarget
console.log(self.cid); // the containing view's CID

关于javascript - 了解 Backbone.js 事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6847182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com