jQuery 悬停仍然触发
我有以下问题。我正在开发一个简单的 jQuery 工具提示,现在我正在处理一些对我来说很奇怪的事情。每次我将鼠标悬停在元素上时,鼠标悬停和鼠标移出的事件都会被触发 - 因此工具提示会消失(但如果我继续移交,它会在一秒钟内闪烁很多次)。这是我的代码。
var hint = $('<div id="hint-wrap"><div id="hintbox-top"><div id="hintbox-bottom"><div id="hintbox-topleft"><div id="hintbox-bottomleft"><div id="hint-innerwrap"><div id="hint"></div></div></div></div></div></div></div>'); // Funny I know :-D
hint.hide();
$('body').append( hint ); // I append it
$('.hashint').hover(function(e){
// Set position to cursor's
hint.css( 'left', e.pageX );
hint.css( 'top', e.pageY - 60 );
// animated append
hint.css( 'opacity', 0.2 );
hint.show();
hint.animate( { 'opacity' : 1 }, 100 );
// set text
$( "#hint" , hint).html( $( '#' + $(this).attr( 'id' ) + '-hint' ).html() );
},
function(){ // there is the problem
hint.hide();
});
还有 HTML:
<div id="usernamelabel-hint" class="hinttext">Lorem Ipsum is simply dummy text of the printing and type.Lorem. <a href="#">Sign up!</a></div> <!-- This is hint text (and code) -->
<p><label><span class="hashint" id="usernamelabel">Username</span><span class="asterisk">*</span></label> <!-- ... stuff --> </p>
请问,有人知道为什么鼠标移出事件仍然触发并隐藏我的框吗?
非常感谢,翁德雷
I have the following problem. I'm working on a simple jQuery tooltip and now I'm dealing with something strange to me. Every time I mouseover the element, events for mouse over and mouse out are triggered both - so the tooltip disappears (but if I keep hand over, it does a lot of blinks in one sec). There's my code.
var hint = $('<div id="hint-wrap"><div id="hintbox-top"><div id="hintbox-bottom"><div id="hintbox-topleft"><div id="hintbox-bottomleft"><div id="hint-innerwrap"><div id="hint"></div></div></div></div></div></div></div>'); // Funny I know :-D
hint.hide();
$('body').append( hint ); // I append it
$('.hashint').hover(function(e){
// Set position to cursor's
hint.css( 'left', e.pageX );
hint.css( 'top', e.pageY - 60 );
// animated append
hint.css( 'opacity', 0.2 );
hint.show();
hint.animate( { 'opacity' : 1 }, 100 );
// set text
$( "#hint" , hint).html( $( '#' + $(this).attr( 'id' ) + '-hint' ).html() );
},
function(){ // there is the problem
hint.hide();
});
And the HTML:
<div id="usernamelabel-hint" class="hinttext">Lorem Ipsum is simply dummy text of the printing and type.Lorem. <a href="#">Sign up!</a></div> <!-- This is hint text (and code) -->
<p><label><span class="hashint" id="usernamelabel">Username</span><span class="asterisk">*</span></label> <!-- ... stuff --> </p>
Please, does anybody know why the mouse out event is still triggering and hiding my box?
Thanks a lot, Ondrej
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是您的工具提示 div 覆盖了您的触发器 div 吗?在这种情况下,出现的工具提示div会触发trigger-div的mouseleave..至少这是我一些div之前发生的事情。我通过用不可见的触发器div覆盖所有内容来解决这个问题 - 不太漂亮,但对我有用..
could it be, that your tooltip-div is overlaying your trigger-div? In that case the appearing tooltip-div triggers the mouseleave of the trigger-div.. At least that's what happend to me some divs ago. I solved that problem by overlaying everything with an invisible trigger-div - not so beautiful, but worked for me..
首先,为什么不将提示的 HTML 放入 HTML 文档中呢?
在 CSS 中你只需设置 display:none ?
然后在显示提示时,您可以使用
hint.show
或hint.fadeIn
。我只会使用 $.mouseenter 和 $.mouseleave。
例子:
First, why don't you just put the HTML for the hint in the HTML document?
And in the CSS you just set display:none ?
Then when showing the hint, you can just use
hint.show
orhint.fadeIn
.I would just use $.mouseenter and $.mouseleave.
Example: