简体   繁体   English

在单选按钮悬停时显示div

[英]Show div on radiobutton hover

Now I have smth like this: http://jsfiddle.net/54tsowq7/107/ 现在我有这样的东西: http : //jsfiddle.net/54tsowq7/107/

I want to save function if radiobutton selected - div with picture is shown. 如果选择单选按钮,我想保存功能-显示带有图片的div。

But how can I make that when radiobutton text is on hover, the same picture is shown? 但是当单选按钮文本悬停时如何显示相同的图片呢? And when mouseleave - it dissapear? 而当mouseleave-它消失了吗?

 $(document).ready(function() { $('input[type="radio"]').change(function() { $('.text, .img').hide(); var $pic = $('#'+$(this).data('picid')); var $text = $('#'+$(this).data('textid')); if ($(this).prop('checked')) { $pic.show(); $text.show(); }else{ $pic.hide(); $text.hide(); } }); }); 
 <div class="img" id="pic2" style='display:none'><img src="http://images.math.cnrs.fr/IMG/png/section8-image.png" alt=""></div> <div class="img" id="pic3" style='display:none'><img src="http://www.simpleimageresizer.com/static/images/simple-image-resizer-128x128.png" alt=""></div> <form class="radiobuttons"> <p class="descr" id="desc1" data-picid="pic2" data-descid="desc1"><input type="radio" name="item1" id="item1" value="1" data-picid="pic2" data-textid="text1">Text 1</p> <p class="descr" id="desc2" data-picid="pic2" data-descid="desc2"><input type="radio" name="item1" id="item2" value="1" data-picid="pic2" data-textid="text2">Text 2</p> <p class="descr" id="desc3" data-picid="pic3" data-descid="desc3"><input type="radio" name="item1" id="item3" value="1" data-picid="pic3" data-textid="text3">Text 3</p> </form> <div class="text" id="text1" style='display:none'>Lorem Ipsum Text1</div> <div class="text" id="text2" style='display:none'>Lorem Ipsum Text2</div> <div class="text" id="text3" style='display:none'>Lorem Ipsum Text3</div> 

You can add mouseenter and mouseout events. 您可以添加mouseentermouseout事件。 But this function will not trigger change event, change event can only trigger when you click on radio button and its attribute or value get changed 但是此功能不会触发change事件, change事件只能在单击单选按钮时触发,并且其属性或值会更改

$('input[type="radio"]').on('mouseenter',function() {
   // do anything on mouse hover
});
$('input[type="radio"]').on('mouseout',function() {
   // do anything on mouse leave
});

It's a little bit messy, but if I correctly understand, you want something like this? 有点混乱,但是如果我正确理解的话,您想要这样的东西吗?

$(document).ready(function() {


 $('input[type="radio"]').hover(
    function() {
    var $pic = $('#'+$(this).data('picid'));
    var $text = $('#'+$(this).data('textid'));
    if(!$(this).prop('checked')) {
      $pic.show();
      $text.show();
        }
    }, function() {
    var $pic = $('#'+$(this).data('picid'));
    var $text = $('#'+$(this).data('textid'));
     if(!$(this).prop('checked')) {
        $pic.hide();
       $text.hide();;
        }

    }
        );

   $('input[type="radio"]').change(function() {
   $('.text, .img').hide();
    var $pic = $('#'+$(this).data('picid'));
    var $text = $('#'+$(this).data('textid'));

    if ($(this).prop('checked')) {
     $pic.show();
     $text.show();
   }else{  
     $pic.hide();
     $text.hide();
   }
});

});

And put your div images to the bottom of your HTML 并将div图片放到HTML的底部

Try this: 尝试这个:

 function handlerInFunc(){ $(".myPicOneClass").show(); } function handlerOutFunc(){ $(".myPicOneClass").hide(); } $(".myRadioOneClass").hover( handlerInFunc, handlerOutFunc ); 

Of course, give the proper class selector names you desire for the specific elements. 当然,为特定元素提供所需的适当的类选择器名称。

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

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