Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示隐藏Divs jQuery(更改每个链接的图像)_Jquery_Html_Hide_Show_Selected - Fatal编程技术网

显示隐藏Divs jQuery(更改每个链接的图像)

显示隐藏Divs jQuery(更改每个链接的图像),jquery,html,hide,show,selected,Jquery,Html,Hide,Show,Selected,我目前正在使用这段代码来显示和隐藏div 我还想有一个自定义的图像精灵为每个链接时,它被选中。我试着给每个链接它自己的ID和背景图片,但它似乎不起作用 <a id=“btn1” class="showSingle" data-target="1">Option 1</a> #btn1 { width:147px; height:172px; background: url(../img/btn1.png) no-repeat top; display:block; }

我目前正在使用这段代码来显示和隐藏div

我还想有一个自定义的图像精灵为每个链接时,它被选中。我试着给每个链接它自己的ID和背景图片,但它似乎不起作用

<a id=“btn1” class="showSingle" data-target="1">Option 1</a>

#btn1 { width:147px; height:172px; background: url(../img/btn1.png) no-repeat top; display:block;  }
#btn1 .selected { background-position:bottom; }

有人能帮我修改/添加此代码以实现我想要的吗?

btn1之间不应该有空格。选中,
它应该是btn1。如果要添加背景图像,请选择该选项,并执行以下操作:

$('.showSingle').on('click', function () {
    $(this).addClass('selected').siblings().removeClass('selected');
    $('.targetDiv').hide();
    $('#div' + $(this).data('target')).show();
    // adding the background.
    var imageURL = 'someURL';
    $('#div' + $(this).data('target')).css("background-image", 'url(' + imageURL + ')');
});
$('.showSingle').first().click();

希望这有帮助

您正在尝试将图像添加到div或?背景?然后将url设置为变量将更改我的解决方案完美!这成功了!看起来只是那个小错误。