如何改变图像使用.on点击事件与4按钮作为触发器
How to change image using .on click event with 4 button as trigger
我需要你们的帮助。这是我的情况。我有4个按钮来触发图像,当按钮点击他们将有活动(我很好与这个-但让我知道如果有更好的方法),
场景:
当我点击按钮2时,我想要"image2"出现(class="active")。
然后,如果我点击按钮3,"image2"淡出(删除类活动),并添加类活动"image3"
假设这是我的4个按钮:
<div id="button">
<div class="trigger button-1" data-target="0"></div>
<div class="trigger button-2" data-target="1"></div>
<div class="trigger button-3" data-target="2"></div>
<div class="trigger button-4" data-target="3"></div>
</div>
和images ()的另一个部分,像这样包装:
<div id="images-container">
<img src="image-1" class="image1">
<img src="image-2" class="image2">
<img src="image-3" class="image3">
<img src="image-4" class="image4">
</div>
我想有某种方式我可以得到图像在jQuery使用数据元素?
我当前的jQuery:
var img = $('#images-container img'),
trigger = $('.trigger');
// click this, make active, other? no
trigger.on('click', function(){
$(this).addClass('active');
$(this).siblings().removeClass('active');
// image that have the selected ID, active.
// other images must remove active classes
var a = $(this).data('target');
$('#image-container').find('img').eq(a).addClass('active');
// and help me from here guys!
我知道这可能很容易,但我不是jQuery专业人士。
每个解决方案都会受到赞赏,真正的解决方案+1
添加数据属性到你的HTML,这涉及到各自的imagecclass…最好选择用addclass…
替代和试试这个