【问题标题】:Mouseenter failing after hovering over a link将鼠标悬停在链接上后 Mouseenter 失败
【发布时间】:2016-08-15 17:25:14
【问题描述】:

当我将鼠标悬停在文本段落上时,我有一个添加信息 div 的功能,但是当我从段落中的普通文本转到超链接时,mouseover 事件停止并且不会重新启动,直到我的鼠标离开段落并重新悬停超过它。如果我从链接开始,它将显示,但如果我从链接转到文本或从文本转到链接,它将始终停止。

段落示例:

<div id="NoteHolder">
    <div class="wrap">
        <p class="NoteOp inline date1471280868332">Example sentence <a target="_blank" class="a" href="https://www.example.com">https://www.example.com</a></p>
    </div>
</div>

当我将鼠标悬停在段落上时,它会将日期编号类转换为人类可读的日期,并显示在 div 中。

鼠标悬停事件:

$('#NoteHolder').on( "mouseenter", ".NoteOp", function() {
    var Info = document.createElement('div');
    Info.className = 'EditInfo'
    var DateP = document.createElement('p');
    DateP.className = 'DivP';
    var DatePT = document.createTextNode(PT)
    DateP.appendChild(DatePT);
    Info.appendChild(DateP);
    var position = $(this).position();
    var thisX = position.left;
    var thisY = position.top;
    var thisWidth = $(this).width();
    var thisHeight = $(this).height();
    var PageW = $('html').width();
    if(thisX + thisWidth + 50 > PageW) {
        $(Info).css({
            "left": thisX + thisWidth + 20,
            "top": thisY - 10
        });
    }
    document.getElementById("NoteHolder").appendChild(Info);
});

$('#NoteHolder').on( "mouseout", ".NoteOp", function() {
    $(".EditInfo").remove();
});

当我将鼠标悬停在链接上时,是否有解决此问题的方法?

【问题讨论】:

标签: javascript jquery html hover


【解决方案1】:

您的代码中存在拼写错误,导致此问题的额外右括号,以下是更正的代码

$(function() {
$('#NoteHolder').on( "mouseover", ".NoteOp", function() {
    alert("Enter");
    var Info = document.createElement('div');
    Info.className = 'EditInfo'
    var DateP = document.createElement('p');
    DateP.className = 'DivP';
    var DatePT = document.createTextNode(PT)
    DateP.appendChild(DatePT);
    Info.appendChild(DateP);
    var position = $(this).position();
    var thisX = position.left;
    var thisY = position.top;
    var thisWidth = $(this).width();
    var thisHeight = $(this).height();
    var PageW = $('html').width();
    if(thisX + thisWidth + 50 > PageW) {
        $(Info).css({
            "left": thisX + thisWidth + 20,
            "top": thisY - 10
        });
    }
    document.getElementById("NoteHolder").appendChild(Info);
   
});

$('#NoteHolder').on( "mouseout", ".NoteOp", function() {
    alert("Exit");
    $(".EditInfo").remove();
});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="NoteHolder">
    <div class="wrap">
        <p class="NoteOp inline date1471280868332">Example sentence <a target="_blank" class="a" href="https://www.example.com">https://www.example.com</a></p>
    </div>
</div>

当您使用 jQuery 时,您可以通过 $("&lt;div/&gt;") 而不是 document.createElement('div') 创建动态 div

【讨论】:

  • 它仍然不起作用,我修复了支架,但我做了一个 jsfiddle,它仍然没有修复它。 jsfiddle.net/ygxvcp3g
  • 当我开始将鼠标悬停在文本上,然后移动并悬停在链接上时仍然无法正常工作。
猜你喜欢
  • 2014-03-28
  • 2015-08-20
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 2017-03-06
相关资源
最近更新 更多