首先,我设置了下面的Fiddle,以展示我想要完成的任务,只使用jQuery:
以下三个事件是有联系的:
$('.test li p').mouseenter(function() {
var el = $(this),
count = ++mouseenters[el.attr('id')];
el.find('.mouseenters .count').text(count);
});
$('.test li p').mouseover(function() {
var el = $(this),
count = ++mouseovers[el.attr('id')];
el.find('.mouseovers .count').text(count);
});
$('.test li p').click(function() {
function r() {
return Math.floor(Math.random() * 256);
}
$(this).css({'background-color': 'rgb(' + r() + ',' + r() + ',' + r()});
});根据您使用的浏览器(我使用的是Chrome),我们看到jQuery正在弥补浏览器供应商对鼠标机缺乏真正的支持。只要鼠标进入或离开子元素,Mouseover就会触发,而mouseenter只在鼠标进入绑定事件的DOM元素2时才会触发。
然后,我用脊骨/Marionette写了以下内容:
事件在Marionette.ItemView中是这样绑定的:
events: {
'mouseover p' : 'handleMouseOver',
'mouseenter p' : 'handleMouseEnter',
'click p' : 'handleClick'
},我们在这里看到,mouseenter和mouseover事件都表现糟糕(或者至少不像行为良好的jQuery版本),而且单击根本没有触发。我当时的印象是主干使用了jQuery事件,但显然我错了,或者简单地说“主干使用jQuery事件”并不能完全解决在幕后发生的事情。
我只想说,我对这里发生了什么很困惑。为什么每次鼠标在主干网版本中移动时鼠标/鼠标都会触发?为什么根本不点击射击?如有解释或解决办法,我们将不胜感激。
发布于 2013-10-16 17:41:41
更新的jsfiddle:http://jsfiddle.net/PhXLf/2/
我相信一切都一样。在主干版本中,每次模型更改时都会重新呈现整个视图。因此,每次鼠标移动时,它都会呈现一个新的div,因此它会注册一个新的“mouseenter”事件。这也防止了“点击”事件的触发,因为单击鼠标会在“鼠标向下”之后触发一个“鼠标中心”,所以当您有一个“mouseup”事件时,它在一个与单击鼠标不同的元素上。在jquery版本中,您只需要更改相关属性,这也是我在本例中要做的。
rerender: function() {
this.$('.mouseovers .count').html(this.model.get('mouseOverCount'));
this.$('.mouseenters .count').html(this.model.get('mouseEnterCount'));
},https://stackoverflow.com/questions/19408929
复制相似问题