【发布时间】:2012-09-27 16:02:51
【问题描述】:
我正在努力实现这个Is there an easy way to toggle background position between two states on click event?
我已经构建了一个应该工作的函数.. 不知道为什么它不会。对于我的项目中如此乏味的部分,我花了几个小时试图实现这一点,但这是必要的。
请看一下这个函数:
$("#secondarrow").click(function(){
/*$(".arrows").toggle(
function(){
$(this).css({"background-position":"0px 0px"});
},
function(){
$(this).css({"background-position":"0px -30px"});
});*/
/*$(".arrows").toggleClass(function() {
if ($(this).is(".arrows")) {
return ".arrowsup";
} else {
return ".arrows";
}
});*/
$('#secondarrow').toggle(function() {
$("#arrow2").css('backgroundPosition', '0px -15px');
}, function() {
$("#arrow2").css('backgroundPosition', '0px 0px');
});
$("#ukmore").slideToggle(100);
$("#clause").slideToggle(100);
});
它应该做的是,在单击 div 名称的第二个箭头时,它会显示许多子类别,并且同时切换具有 id #arrow2 的 div 子项的背景位置从指向下方的箭头(表示点击显示更多)到作为同一背景图像一部分的向上箭头(表示点击隐藏子类别)。
我尝试了许多不同的函数结构,但它们都没有按要求工作,最新的一个 - 没有被注释掉的 - 确实改变了背景位置以显示向上的箭头,但它只是这样做在第一次点击时,只有在直接点击 #arrow1 div 时才会这样做,当我需要它在点击 #secondarrow div 时来回切换。
请帮忙。
【问题讨论】:
-
可能是一个愚蠢的问题,但它不应该是“背景位置”而不是“背景位置”吗?你也可以做一个jsFiddle吗?
-
@Antimated 别担心,我一直在问这种愚蠢的问题,你不问你不知道。不,这很好 - 我相信这是正确的 jQuery 语法,但也可以。如果您查看上一个问题,有很多内容可以为我的问题提供背景信息,包括图像和一些不起作用的解决方案。
-
你说一个id为#arrow1的div子,你使用了类选择器(.arrow1)。尝试改变它
-
@AndreiR 抱歉,是的,我发现并在发布后更改了它,但仍然没有用。已编辑问题中的代码。
标签: jquery toggle background-position