简体   繁体   English

在鼠标上方显示和隐藏元素

[英]show and hide element on mouse over jquery

I want to show the buttons on hover and hide them when moused out, i tried the following code but doesnt work. 我想显示鼠标悬停时的按钮,并在鼠标移开时隐藏它们,我尝试了以下代码,但不起作用。

<div class="mybooks">
    <div class="divbutton">
         <input  class="btnsubmit"  type="button" value="Edit" id="edit_trivia">
         <input  class="btnsubmit"  type="button" value="Delete" id="delete_trivia">
    </div>
</div> 
".$Tri_IMAGE."
".$Tri_CAPTION."
</div>";
}
?>
 </div>
      <!--close accordion-->
   </div>
   <script>
   $( ".mybooks" ).mouseenter(function() {
      $('.divbutton').show();
  });

  $( ".mybooks" ).mouseleave(function() {
  $('.divbutton').hide();
 });

 </script>

Just hide the div first 先隐藏div

//hide the div here
var $btn = $('.divbutton').hide()
$(".mybooks").mouseenter(function () {
    $btn.show();
});

$(".mybooks").mouseleave(function () {
    $btn.hide();
});

Demo: Fiddle 演示: 小提琴


Or use a css rule to hide it 或使用CSS规则将其隐藏

.divbutton {
    display:none
}

Demo: Fiddle 演示: 小提琴

You should put your javascript code in this function: 您应该将javascript代码放在此函数中:

$( document ).ready(function() {
    //your code
});

Your code: http://jsfiddle.net/VGJ5u/ 您的代码: http : //jsfiddle.net/VGJ5u/

This fiddle shows the buttons hiding ( display: none; ) when you hover over them. 小提琴显示了将鼠标悬停在按钮上时隐藏的按钮( display: none; )。 The thing is, you can't have them reappear once they're gone. 问题是,一旦它们消失,就无法让它们重新出现。 There is nothing to hover over... 没有什么可以悬停...

<div class="divButtons">
    <input  class="btnsubmit"  type="button" value="Edit" id="edit_trivia" />
    <input  class="btnsubmit"  type="button" value="Delete" id="delete_trivia" />
</div>

JavaScript: JavaScript的:

$('#edit_trivia').hover(function(){
    $(this).css('display', 'none');
});

$('#delete_trivia').hover(function(){
    $(this).css('display', 'none');
});

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

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