如何在下拉列表的锚标签上触发鼠标悬停事件

时间:2021-07-26 20:27:15

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}
.dropbtn:hover,
.dropbtn:focus {
  background-color: #3e8e41;
}
.dropdown {
  position: relative;
  display: inline-block;
}
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #0000FF;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
  color: white;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}
.dropdown a:hover {
  background-color: #ff0000
}
.show {
  display: block;
<div class="dropdown">
  <button id="myButton" onclick="PrintHello()" class="dropbtn">click</button>
  <div id="myDropdown" class="dropdown-content">
    <a class="link" id="Home" href="#home">Home</a>
    <a class="link" id="About1" href="#about">About</a>
    <a class="link" id="Contact" href="#contact">Contact</a>
  </div>
</div>

i'm trying to trigger mouse 'hover' event on one of the anchor tag when the drop down menu pop-ups,

我在下拉菜单弹出窗口时尝试在其中一个锚标签上触发鼠标'悬停'事件,

i'm using

$("#About1").trigger('hover');

it doesnt trigger any event. i have written some event handler for anchor tag for "hover"event. when i use $("#About1").trigger('mouseover'); it calls the event handler but there is no hover effect seen on the webpage. i was expecting it to be hovered as same as when u physically hover the mouse on it. am I missing something ? is it possible to create such effects by triggering the events ? please do suggest.

它不会触发任何事件。我为“悬停”事件编写了一些锚标记的事件处理程序。当我使用$(“#About1”)。触发器('mouseover');它调用事件处理程序,但在网页上没有看到悬停效果。我期待它像你将鼠标悬停在它上面一样徘徊。我错过了什么吗?是否可以通过触发事件来创建此类效果?请建议。

3 个解决方案

#1


2  

You can just use hover() DEMO

你可以使用hover()DEMO

$('button, #myDropdown').hover(function() {
  $('#myDropdown').toggleClass('show');
})

Or you can trigger custom event on when you hover (mouseenter) over element DEMO

或者,当您将鼠标悬停在元素DEMO上时,可以触发自定义事件

$('#About1').mouseenter(function() {
  $(this).trigger('customEvent');
});

$('#About1').on('customEvent', function() {
  alert('Lorem Ipsum');
});

#2


0  

If you try to change styles only it's better to use selector :hover in css.

如果您尝试更改样式,最好使用选择器:hover in css。

If you require anything else use hover: $('selector').hover(functionOnHover,FunctionOnHoverOut);

如果你需要其他任何东西,请使用hover:$('selector')。hover(functionOnHover,FunctionOnHoverOut);

#3


0  

If you want to trigger mouse hover event on drop down items like for example "About" without physically hovering the mouse on it, you can add

如果您想在下拉项目上触发鼠标悬停事件,例如“关于”而不将鼠标悬停在其上,则可以添加

$(document).ready(function(){ $("#About1").trigger('mouseenter'); });

$(document).ready(function(){$(“#About1”)。trigger('mouseenter');});

DEMO

#1


2  

You can just use hover() DEMO

你可以使用hover()DEMO

$('button, #myDropdown').hover(function() {
  $('#myDropdown').toggleClass('show');
})

Or you can trigger custom event on when you hover (mouseenter) over element DEMO

或者,当您将鼠标悬停在元素DEMO上时,可以触发自定义事件

$('#About1').mouseenter(function() {
  $(this).trigger('customEvent');
});

$('#About1').on('customEvent', function() {
  alert('Lorem Ipsum');
});

#2


0  

If you try to change styles only it's better to use selector :hover in css.

如果您尝试更改样式,最好使用选择器:hover in css。

If you require anything else use hover: $('selector').hover(functionOnHover,FunctionOnHoverOut);

如果你需要其他任何东西,请使用hover:$('selector')。hover(functionOnHover,FunctionOnHoverOut);

#3


0  

If you want to trigger mouse hover event on drop down items like for example "About" without physically hovering the mouse on it, you can add

如果您想在下拉项目上触发鼠标悬停事件,例如“关于”而不将鼠标悬停在其上,则可以添加

$(document).ready(function(){ $("#About1").trigger('mouseenter'); });

$(document).ready(function(){$(“#About1”)。trigger('mouseenter');});

DEMO