如果在鼠标悬停事件中触发了鼠标关闭,则立即返回,并且不执行链接的鼠标退出事件
Return immediately and not execute chained mouseout event if mousedown was triggered inside mouseover event
不知道该怎么做,但基本上我写了一个工具提示插件,它删除了mouseout
或mousedown
上的工具提示。
如果触发了mousedown
事件,它将删除$that.parent()
这很好,这会删除工具提示,但是如果用户也触发了mouseout事件(他们会这样做,因为mouseover
和mouseout
事件当前是链接的),然后它将删除另一个我不想要的DOM元素。所以本质上我想知道这是否可能:
$that.on('mouseover', function() {
// If this event is triggered within the mouseover event, don't run the chained mouseout event
$that.on('mousedown', function() {
$that.parent().next().fadeOut(100).remove();
return false;
});
}).mouseout(function() {
// If they clicked above, don't run this
$that.parent().next().fadeOut(100).remove();
});
据我所知,如果不使用全局变量,就很难访问该mousedown
事件中的clicked
布尔集,例如:
$that.on('mouseover', function() {
clicked = false;
// If this event is triggered within the mouseover event, don't run the chained mouseout event
$that.on('mousedown', function() {
clicked = true;
$that.parent().next().fadeOut(100).remove();
return false;
});
}).mouseout(function() {
// If they clicked above, don't run this
if (clicked) {
$that.parent().next().fadeOut(100).remove();
}
});
关于如何优雅地构建它的任何想法?
编辑:$that.parent().next()
中的元素只是一个<div class="js-tooltip"><span>Tooltip text</span></div>
但这应该是无关紧要的,因为我只想知道如果在不使用全局变量的情况下触发mousedown
是否可以从该mouseover
函数返回。
你不需要mouseover
.
$that.on('mouseleave mouseup', function(e) {
if( e.type === 'mouseleave'){
// do the mouseleave stuff
}else{
// do the mouseup stuff
}
});
正如你所说,如果元素是动态创建的,你应该使用:
$(document).on('mouseleave mouseup', $that, function(e) {
您是否考虑过像这样简单地使用类过滤器?
$that.parent().next('.js-tooltip').fadeOut(100).remove();
如果下一个不是工具提示,这将根本无济于事,据我所知,这应该可以解决问题。
使用您提出的方法,做$that.clicked = false
会更清晰。
或者怎么样(如果你想保持鼠标悬停 - 这只是为了演示原理;我不确定它是否会像这样工作):
$that.on('mouseover', function() {
// If this event is triggered within the mouseover event, don't run the chained mouseout event
$that.on('mousedown mouseout', function() {
$that.parent().next().fadeOut(100).remove();
$that.off('mousedown mouseout'); //prevent that happening again
return false;
});
});
相关文章:
- 节点导出返回一个空对象
- ES6构造函数返回基类的实例
- JQuery BlockUI鼠标加载光标没有't在Google Chrome中返回默认值
- 面料:“;鼠标:向下”;在event.target中未返回对象
- 单击时更改鼠标指针,释放时返回
- Ajax 请求生成具有后续 Ajax 请求潜力的 html.辅助请求返回 [对象鼠标事件] 而不是生成的值
- 在鼠标退出后,JQuery 无法将某些内容返回到其原始位置
- 如果在鼠标悬停事件中触发了鼠标关闭,则立即返回,并且不执行链接的鼠标退出事件
- D3 X 值鼠标悬停 - 函数返回 NaN
- 如何获得EaselJS'鼠标事件是否正常工作?它看起来可以工作,但它不断返回一个位图ID,而不是正确的
- 用于获取鼠标坐标的javascript onunload函数在IE10紧凑视图中返回负值
- 使用Javascript鼠标事件返回HTML5画布上鼠标点击的坐标
- jQuery如何使用变量返回鼠标位置
- 我如何让Javascript画布返回x和y鼠标位置
- 鼠标滚轮事件总是返回小于0(向下)在firefox?JQuery
- 鼠标退出后返回到原始图像源
- 返回鼠标悬停按钮的标题
- 鼠标流跟踪返回匿名函数
- Javascript:事件.哪个/按钮在鼠标上返回错误的值
- Onclick循环中的解决方法总是返回鼠标事件