2012-06-03 108 views
1

我有一个函数,当你悬停在具有类“评论”的div淡出回复按钮。制作按钮后点击fadeIn()

我有正在按钮点击时,它在淡出的问题。在这个

$(".comment").hover(
     function() { 
      $(this).children(".commentOptions").fadeIn(); 
      $(this).children("#reply").live('click',function(){ 
       alert('clicked'); 
      }); 
     }, 
     function() { 
      $(this).children(".commentOptions").fadeOut(); 
     }); 



<div id="comment" class="comment"> 
     <div class="commentOptions"> 
      <div class="reply"> 
      <div class="button" id="reply">Reply</div> 
      </div> 
     </div> 
</div> 

任何帮助将非常学徒,谢谢。 :)

回答

0
$(".comment").hover(
    function() { 
     $(this).children(".commentOptions").fadeIn(1000, function(){ 
      $("#reply").on("click", replyClicked); 
     }); 
    }, 
    function() { 
     $(this).children(".commentOptions").fadeOut(1000, function(){ 
      $("#reply").off("click", replyClicked); 
     }); 
}); 

function replyClicked(){ 
    alert("Clicked"); 
} 

http://jsfiddle.net/S7rxh/

0

根据您的标记,#reply不是.comment子元素,所以为了从.comment解决它,你必须使用类似$("#reply", this)或简单$("#reply")(因为ID应该是唯一的)。而且,每次你将鼠标悬停时,都不需要绑定事件,你可以做一次。下面是一个例子:

$(".comment").hover(function() { 
    $(this).children(".commentOptions").fadeIn(); 
}, function() { 
    $(this).children(".commentOptions").fadeOut(); 
}); 

$("#comment").on("click", "#reply", function() { 
    alert('clicked'); 
});​