Javascript jQuery在一个元素上触发mouseenter,而实际上没有真正的鼠标进入元素

Javascript jQuery在一个元素上触发mouseenter,而实际上没有真正的鼠标进入元素,javascript,jquery,css,mouseenter,mouseleave,Javascript,Jquery,Css,Mouseenter,Mouseleave,我有一个包含元素的网页,当鼠标悬停在元素上时,元素中会出现文本。 我想生成鼠标悬停/mouseenter来显示文本,即使没有鼠标实际悬停在元素上。 代码,例如: HTML 我想生成一个mouseenter,它在jQuery中触发$hover_to_show_text.mouseenterfunction{,并在那里停留N秒 我分别试过: $("#hover_to_show_text").hover(); $("#hover_to_show_text").trigger('mouseover');

我有一个包含元素的网页,当鼠标悬停在元素上时,元素中会出现文本。 我想生成鼠标悬停/mouseenter来显示文本,即使没有鼠标实际悬停在元素上。 代码,例如:

HTML

我想生成一个mouseenter,它在jQuery中触发$hover_to_show_text.mouseenterfunction{,并在那里停留N秒

我分别试过:

$("#hover_to_show_text").hover();
$("#hover_to_show_text").trigger('mouseover');
$("#hover_to_show_text").trigger('mouseenter');
没用,有办法吗


谢谢。

应该有效。事件几乎会立即触发。因此,您可能没有看到差异

$( "#hover_to_show_text" ).mouseenter(function() {
   $('#hidden_text').show();
});
 $( "#hover_to_show_text" ).mouseleave(function() {
   $('#hidden_text').hide();
});

$("#hover_to_show_text").trigger('mouseover');

setTimeout(function() {
    $("#hover_to_show_text").trigger('mouseleave');
}, 1500);
将mouseleave的tiggering封装在setTimeout中以查看差异

$( "#hover_to_show_text" ).mouseenter(function() {
   $('#hidden_text').show();
});
 $( "#hover_to_show_text" ).mouseleave(function() {
   $('#hidden_text').hide();
});

$("#hover_to_show_text").trigger('mouseover');

setTimeout(function() {
    $("#hover_to_show_text").trigger('mouseleave');
}, 1500);
更新


我不确定我是否完全理解你的问题。但在将代码放入JSFIDLE后,它似乎成功地触发了鼠标悬停和鼠标移动。我认为你做得不对。你的代码运行良好……你确定你的项目中有jquery吗?上面的代码中有拼写错误。和$hover_to_show_text.triggermouseinter;应该可以工作。@epascarello itdosnt工作。例如,在堆栈溢出中,当您将鼠标悬停在菜单中的用户上方时,它的背景将是橙色的。但是,如果您运行$nav-users.triggermouseinter,则不会发生任何事情。因为用户使用CSS psudeo:hover,这不是JavaScript事件。您不会触发它。@batz第三个元素是什么意思。我是指其他元素例如,rr按钮将触发它。如:@batz Check Update comment。
$( "#hover_to_show_text" ).mouseenter(function() {
   $('#hidden_text').show();
});
 $( "#hover_to_show_text" ).mouseleave(function() {
   $('#hidden_text').hide();
});

$("#hover_to_show_text").trigger('mouseover');

setTimeout(function() {
    $("#hover_to_show_text").trigger('mouseleave');
}, 1500);
// Just triggering the click event will not work if no
// click handler is provided.
// So create one first
$("#btn").on('click', function () {

    // trigger the mouse enter event 
    $("#hover_to_show_text").trigger('mouseenter');

    setTimeout(function () {
        $("#hover_to_show_text").trigger('mouseleave');
    }, 1500);

});