简体   繁体   English

点击切换图片(jQuery)

[英]Toggle Images on Click (jQuery)

I have a navigation button and I want to change the image everytime the user clicks on it (from the Menu icon to the Close icon), this is the code I'm currently using but when I swap the text to images the Close icon doesn't "go back" to the Menu icon for some reason. 我有一个导航按钮,我想每次用户单击时都更改图像(从“菜单”图标到“关闭”图标),这是我当前使用的代码,但是当我将文本交换为图像时,“关闭”图标却没有由于某种原因,请不要“返回”菜单图标。

Any help? 有什么帮助吗? Thanks. 谢谢。

 $(function() { $('.mobileMenu').on('click', function(e) { $('body').toggleClass('menuOpened'); $(this).html($(this).html() == 'Close' ? 'Menu' : 'Close'); }); }); 
 <div class="mobileMenuRight"> <a href="/cart" class="mobileCart"><img src="../cartIcon.png" alt="Cart" height="22"></a> </div> 

Hope this is what you are looking for. 希望这是您想要的。

 $('.mobileMenuRight').click( function() { if (!$('.mobileMenuRight').hasClass('menuOpened')) { $('.mobileMenuRight').addClass('menuOpened'); $('.mobileCart ' + 'img').attr({ src: "http://via.placeholder.com/250x250/000000/8888888", }); } else { $('.mobileMenuRight').removeClass('menuOpened'); $('.mobileCart ' + 'img').attr({ src: "http://via.placeholder.com/250x250/8888888/000000", }); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mobileMenuRight"> <a href="#" class="mobileCart"><img src="http://via.placeholder.com/250x250/8888888/000000" alt="Cart" height="22"></a> 

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

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