开发者

Jquery :not()selector

<html lang="en">
<head>
<style>
#thisdiv {padding:10px; margin:-15px 0 0 40px; background-color:#333; position:absolute; display:none;}
div.block {margin-top:10px; width:200px;}
div#thisdiv a {color:#fff;}
</style>
</head>
<body>

<div id ="one" class="block"><a href="">one</a></div>
 <div id="thisdiv">hello</div>
<div id ="two"class="block">two</div>
<div id ="three"class="block">three/div>


<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
  $('#one').hover(function() {
   $('#thisd开发者_开发问答iv').fadeIn(400);
  }); 

  $('div:not(#thisdiv)').mouseleave(function() {
   $('#thisdiv').fadeOut(400) 
  });
});
</script>
</body>
</html>

#thisdiv doesn't fadeOut. I could have used the following, but it only fades out if the cursor mouse outs of #thisdiv. Is there any way i can solve this so when the cursor navigates away from anyway, the div still fade out.

$('#thisdiv').mouseleave(function() {
   $('#thisdiv').fadeOut(400) 
  });

I couldn't figure out what's why jquery's :not selector is not doing what I wanted to. Am i using it wrongly?


1 - You need to use mouseover, not hover for the first binding:

  $('#one').mouseover(function() {
     $('#thisdiv').fadeIn(400);
  }); 

hover accepts two function parameters (mouseover/mouseout).

2 - Your closing div tag at the end of your markup is broken (missing a <):

<div id ="three"class="block">three/div> <-- here

I tested your code having made the above modifications, and it seems to work as you want it to (if I've understood correctly). Test it here.


if you are going to explicitly set the time, you will need to pass it in as a hash parameter. Otherwise you can use 'slow', 'fast' or the default which is 400.

$('#my_button').mouseleave(function(){ $(this).fadeOut({duration:1000}) })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜