Jquery LIVE 带有附加功能,正在破坏 MouseOver
代码如下:
<p>Morbi vitae erat. Cras sem lorem, porta ut, aliquam id, porta sed, velit.
Pellentesque scelerisque erat rhoncus nulla. <span class="findme">find me</span>Integer pulvinar, est ut</p>
<script type="text/javascript">
$(document).ready(function() {
$('.findme').live('mouseover mouseout', function(event) {
if (event.type == 'mouseover') {
// do something on mouseover
$(this).css("background", "red");
$(this).append('<span id="dropdown">XXX</span>');
} else {
// do something on mouseout
$(this).css("background", "transparent");
$('#dropdown').remove();
}
});
});
</script>
我希望在下一个元素旁边出现一个下拉元素,以允许用户在将鼠标移到上方时更改设置。问题是,当鼠标滚动到 XXX 上时,它会触发鼠标移出,即使它位于 .findme 内部,有什么想法吗?或者有更好的方法来实现这种效果?
Here's the code:
<p>Morbi vitae erat. Cras sem lorem, porta ut, aliquam id, porta sed, velit.
Pellentesque scelerisque erat rhoncus nulla. <span class="findme">find me</span>Integer pulvinar, est ut</p>
<script type="text/javascript">
$(document).ready(function() {
$('.findme').live('mouseover mouseout', function(event) {
if (event.type == 'mouseover') {
// do something on mouseover
$(this).css("background", "red");
$(this).append('<span id="dropdown">XXX</span>');
} else {
// do something on mouseout
$(this).css("background", "transparent");
$('#dropdown').remove();
}
});
});
</script>
I want a dropdown element to appear next to the next, to allow the user to change a setting when they move their mouse over. Problem is that when the mouse rolls over the XXX, it triggers a the mouseout, even though it's inside the .findme Any ideas why that is? Or a better way to accomplish this effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是
mouseout
的标准行为。如果您使用的是 jQuery 1.4,那么您应该将 mouseover / mouseout 替换为 mouseenter / mouseleave。编辑:
一些示例代码:
This is the standard behavior of
mouseout
. If you are using jQuery 1.4 then you should replace mouseover / mouseout with mouseenter / mouseleave.EDIT:
Some example code:
尝试使用 mouseenter 和 mouseleave。
Try using mouseenter and mouseleave.
最终工作正常:
希望这可以帮助其他有类似需求的人。
This ended up working correctly:
Hopefully this helps others with a similar need.