我的jQuery在鼠标悬停时会将气泡换成更彩色的图形,但我想不出如何添加一个“点击”功能来突出显示用户选择的气泡。如果用户点击其中一个气泡,它应该停留在彩色悬停图形中。这有意义吗?
我的HTML设置了4个图像作为页面导航。它们是图形气泡。我给了每个气泡它自己的ID。
$( ".WORLD-BTNS" ).hover(
function() {
var id = $(this).attr('id');
$(this).attr("src","images/buttons/" + id + "-BTN-HOVER.png");
}, function() {
var id = $(this).attr('id');
$(this).attr("src","images/buttons/" + id + "-BTN.png");
}
)<img class="WORLD-BTNS" id="COLOR" src="images/buttons/COLOR-BTN.png" />
<img class="WORLD-BTNS" id="SKINCARE" src="images/buttons/SKINCARE-BTN.png" />
<img class="WORLD-BTNS" id="FRAGRANCE" src="images/buttons/FRAGRANCE-BTN.png" />
<img class="WORLD-BTNS" id="HAIRCARE" src="images/buttons/HAIRCARE-BTN.png" />发布于 2017-08-02 05:16:56
您可以通过让您的单击处理程序向有问题的元素添加一个类,然后让您的鼠标悬停函数排除具有该类的元素来解决此问题。
像这样的东西应该是有效的:
$('.some-container')
.on('mouseover', '.WORLD-BTNS:not(.selected)', function() {
this.src = 'http://via.placeholder.com/100x100?text='+this.id+' hovered';
})
.on('mouseleave', '.WORLD-BTNS:not(.selected)', function() {
this.src = 'http://via.placeholder.com/100x100?text='+this.id+' normal';
})
.on('click','.WORLD-BTNS:not(.selected)',function() {
var $this=$(this);
$(".WORLD-BTNS.selected")
.attr('src', 'http://via.placeholder.com/100x100?text='+this.id+' normal')
.removeClass('selected');
$this.addClass('selected');
this.src = 'http://via.placeholder.com/100x100?text='+this.id+' selected';
}); img{
margin:10px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="some-container">
<img class="WORLD-BTNS" id="COLOR" src="http://via.placeholder.com/100x100?text=normal" data-alt-src="http://via.placeholder.com/100x100?text=selected"/>
<img class="WORLD-BTNS" id="SKINCARE" src="http://via.placeholder.com/100x100?text=normal" data-alt-src="http://via.placeholder.com/100x100?text=selected" />
<img class="WORLD-BTNS" id="FRAGRANCE" src="http://via.placeholder.com/100x100?text=normal" data-alt-src="http://via.placeholder.com/100x100?text=selected" />
<img class="WORLD-BTNS" id="HAIRCARE" src="http://via.placeholder.com/100x100?text=normal" data-alt-src="http://via.placeholder.com/100x100?text=selected" />
</div>
发布于 2017-08-02 22:11:35
这是完美的工作,测试它并回答我,请:
$("document").ready(function(){
sessionStorage.clear();
$( ".WORLD-BTNS" ).on("click", function() {
var ai= $(".WORLD-BTNS").length;
for (var i=0 ; i<ai ; i++) {// Clear anyone clicked before.
$(".WORLD-BTNS").eq(i).attr("src", "images/buttons/"
+ $(".WORLD-BTNS").eq(i).attr("id") + "-BTN.png");
}
var id = $(this).attr('id') ;
$(this).attr("src", "images/buttons/" + id + "-BTN-HOVER.png");
sessionStorage.setItem("clicked",id);
});
$(".WORLD-BTNS").hover(function () {// Hover effects.
var id = $(this).attr('id') ;
$(this).attr("src", "images/buttons/" + id + "-BTN-HOVER.png");
} , function () {
var id = $(this).attr('id') ;
if(sessionStorage.getItem("clicked")!=id)
$(this).attr("src", "images/buttons/" + id + "-BTN.png");
});
});
<html>
<img class="WORLD-BTNS" id="COLOR" src="images/buttons/COLOR-BTN.png" />
<img class="WORLD-BTNS" id="SKINCARE" src="images/buttons/SKINCARE-BTN.png" />
<img class="WORLD-BTNS" id="FRAGRANCE" src="images/buttons/FRAGRANCE-BTN.png" />
<img class="WORLD-BTNS" id="HAIRCARE" src="images/buttons/HAIRCARE-BTN.png" />PS。我的想法是:保留被点击的项ID以供以后检查,我建议使用"sessionStorage“作为前面的示例,以及
https://stackoverflow.com/questions/45447674
复制相似问题