0

I have a problem with the following code:

<li>
    <a href="http://example.com" id="webd">
        <span class="hot">W</span>eb Development
    </a>
</li>
..................
<p class='web' > Web for all</p>

CSS file :

.hover {
  background: #000;
}

.hot{
    text-decoration:underline;
}`

Javascript file :

 $('body').keypress(function(event){
    if(String.fromCharCode(event.which)=="w" || String.fromCharCode(event.which)=="W")
    {
        $('#webd').hover();
    }........
    .....
    }
    $('#webd').hover(function(event)
    {
      $('.web').show();
      $('.prog').hide();
      $('.rdbms').hide();
      $('#webd').addClass('hover');
    }, function()
    {
      $('#webd').removeClass('hover');
    });

Even though with

alert(String.fromCharCode(event.which)) 

I get w or W manually calling hover() doesn't work. The text 'Web for all' isn't being displayed....

Can anyone explain that? Thank you in advance!

4

1 回答 1

2

hover 不是事件,它是注册 mouseenter 和 mouseleave 事件处理程序的简写

所以触发 mouseenter/mouseleave 事件

$('#webd').mouseenter();//or $('#webd').trigger('mouseenter');

演示:小提琴

于 2013-09-02T08:44:14.890 回答