简体   繁体   English

我现在将鼠标悬停在元素上,一旦鼠标移出,我想将其隐藏。 我该怎么做呢?

[英]I have an element show on mouseover now I would like to hide it once I mouse out. How do I do this?

I am using this HTML: 我正在使用以下HTML:

<div class="menu">
    <ul>
        <li><a href="#" name="div1" >Home</a></li>
        <li><a href="#" name="div2" >Page1</a></li>
        <li><a href="#" name="div3" >page2</a></li>
        <li><a href="#" name="div4" >page3</a></li>
    </ul>
</div>

<div>

    <div class="div1" style="display:none">
        Test
    </div>

    <div class="div2" style="display:none">
        Test...
    </div>

    <div class="div3" style="display:none">
        Test 1
    </div>

    <div class="div4" style="display:none">
        Test 2
    </div>

</div>

Along with the following jQuery: 以及以下jQuery:

$('a').mouseover

(function() {
    var divname = this.name;
    $("." + divname).show("slow");
});

JsFiddle JsFiddle

I want to be able to mouseout and hide once I leave the boxes that appear not on "a". 我希望能够将鼠标移出并隐藏,直到我离开不在“ a”上显示的框。 How do I do this? 我该怎么做呢?

Wow Im sorry I didnt understand the question initially I guess, see below. 哇,对不起,我猜我最初不理解这个问题,请参阅下文。 This binds mouseout to the div you just showed, so it will stay until you move out of it, not the a . 这会将mouseout绑定到您刚刚显示的div ,因此它将一直保留到您移出它为止,而不是a

Live Demo 现场演示

$('a').mouseover(function() {
    var divname = this.name;
    $("." + divname).show("slow").mouseout(function(){$(this).hide('slow')});
});

I would recommend changing your markup however to have the div's as children of the li's, or at the very least, putting them closer to the a's once hovered over because if you notice you can hover over all of the a elements but never mouseout of the div causing them to stay. 我建议您将标记更改为将div作为li的子代,或者至少将它们置于悬停的a的附近,因为如果您注意到可以将鼠标悬停在所有a元素上,但请勿将其移出导致他们留下的div。 Then you could do something like the following: 然后,您可以执行以下操作:

$('li').hover(function() {
    var divname = this.name;
    $("." + divname).show("slow");
},function(){
    var divname = this.name;
    $("." + divname).hide("slow");
});

http://jsfiddle.net/genesis/r739G/1/ http://jsfiddle.net/genesis/r739G/1/

$('a').mouseover(function() {
    var divname = this.name;
    $("." + divname).show("slow");
});

$('a').mouseout(function(){
    var divname = this.name;
    $("." + divname).hide("slow");
});

Add this.... 加上这个...

(function() {
    var divname = this.name;
    $("." + divname).hide("slow");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM