-2
jq(".1st").click(
  function() {   
      jq(this).next("div").slideToggle("normal");
      jq(this).toggleClass(this).attr("id","standfeat2");
  }
);

当我单击下拉按钮时,它会下拉一条消息,然后我希望它在再次单击时切换回原始类“standfeat”。我发誓我什么都试过了,我知道这很简单。我在 2 个 css id 之间切换。一个有 + 一个有 - 。感谢您提前提供的所有帮助!

这是我的 CSS:

#standfeat {
    color:#185596; 
    font-size:24px; 
    padding-left:40px; 
    background: url(../images/standfeat.png) no-repeat 4px 50%; 
    line-height:24px; 
    cursor:pointer; 
    margin:30px 0px; 
} 

#standfeat2 { 
    color:#185596; 
    font-size:24px; 
    padding-left:40px; 
    background: url(../images/standfeat2.png) no-repeat 4px 50%; 
    line-height:24px; 
    cursor:pointer; 
    margin:30px 0px; 
} ​
4

3 回答 3

1

我假设你的意思是类,而不是 ID。toggleClass不会在两个类之间切换,它会添加或删除一个类 f.ex:

jq(this).toggleClass('open');

第一次调用此函数时,它会添加 class open。下次删除它时,等等。您可以在 CSS 中使用该逻辑来设置不同状态的样式,f.ex:

li{ background: url('plus.png') no-repeat; }
li.open{ background-image: url('minus.png'); }

如果您真的想切换 ID,您可以执行以下操作:

this.id = this.id == 'standfeat' ? 'standfeat2' : 'standfeat';
于 2012-08-27T14:40:44.807 回答
1

在单击时修改元素 id 可能不是一个好主意,为此目的使用类会更干净(这就是为什么有toggleClass方法但没有toggleId方法的原因)。

将您的 CSS 修改为:

.standfeat { color:#185596; font-size:24px; padding-left:40px; background: url(../images/standfeat.png) no-repeat 4px 50%; line-height:24px; cursor:pointer; margin:30px 0px; }
.standfeat2 { color:#185596; font-size:24px; padding-left:40px; background: url(../images/standfeat2.png) no-repeat 4px 50%; line-height:24px; cursor:pointer; margin:30px 0px; } 

然后就可以使用下面的jsL

jq(".1st").click(
    function() {   
        jq(this).next("div").slideToggle("normal");
        jq(this).toggleClass("standfeat standfeat2");
    }
);

工作演示(不包括您的图像)

于 2012-08-27T14:46:28.830 回答
0

您需要将类名传递给toggleClass(),而不是对object.

jq(".1st").click(
  function() {   
      jq(this).next("div").slideToggle("normal");
      jq(this).toggleClass("someClassName").attr("id","standfeat2");
  }
);
于 2012-08-27T14:38:43.263 回答