jQuery 兄弟/removeClass“active”只是行不通
我一直在尝试让这个箭头切换脚本在我的页面上工作。我想我错过了一些简单的东西,但我只是不知道它是什么。花了2个小时才弄清楚这个问题。
脚本:
$(document).ready(function(){
$(".accordion h3:first").addClass("active");
$(".accordion a:first").addClass("active");
$(".accordion p:not(:first)").hide();
$(".accordion h3").click(function()
{
$(this).next("p").slideDown("slow")
.siblings("p:visible").slideUp("slow");
});
$(".accordion a").click(function()
{
$(this).css({background:"url(images/arrowdown.gif) bottom right no-repeat"});
$(this).siblings("a").removeClass("active");
});
});
css:
.accordion h3 a {
padding:20px 0 0 190px;
display:block;
background:url(images/arrowup.gif) no-repeat bottom right; }
.accordion h3 a:hover { text-decoration:none; }
.accordion h3 a.active {
background:url(images/arrowdown.gif) no-repeat bottom right; }
然后我的 HTML:
<div class="accordion">
<h3 id="tab1">
<a href="#">Title 1</a>
</h3>
<p>Description Title 1</p>
<h3 id="tab2">
<a href="#">Title 2</a>
</h3>
<p>Description Title 2</p>
<h3 id="tab3">
<a href="#">Title 3</a>
</h3>
<p>Description Title 3</p>
</div>
所以 up &向下箭头位于“a href”标签内,H3 标签中有不同的背景图像,具体取决于“tab”ID。我希望这是有道理的。
先感谢您!!
I've been trying to get this arrow toggle script work on my page. I think i miss something simple but I just don't know what is it. Been spending 2 hrs to figure this out.
The script :
$(document).ready(function(){
$(".accordion h3:first").addClass("active");
$(".accordion a:first").addClass("active");
$(".accordion p:not(:first)").hide();
$(".accordion h3").click(function()
{
$(this).next("p").slideDown("slow")
.siblings("p:visible").slideUp("slow");
});
$(".accordion a").click(function()
{
$(this).css({background:"url(images/arrowdown.gif) bottom right no-repeat"});
$(this).siblings("a").removeClass("active");
});
});
css:
.accordion h3 a {
padding:20px 0 0 190px;
display:block;
background:url(images/arrowup.gif) no-repeat bottom right; }
.accordion h3 a:hover { text-decoration:none; }
.accordion h3 a.active {
background:url(images/arrowdown.gif) no-repeat bottom right; }
Then my HTML :
<div class="accordion">
<h3 id="tab1">
<a href="#">Title 1</a>
</h3>
<p>Description Title 1</p>
<h3 id="tab2">
<a href="#">Title 2</a>
</h3>
<p>Description Title 2</p>
<h3 id="tab3">
<a href="#">Title 3</a>
</h3>
<p>Description Title 3</p>
</div>
So the up & down arrow is INSIDE the "a href" tag and there is a different background image in H3 tag depends on the "tab" ID. I hope that make senses.
Thank you in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
siblings
适用于同一父级下的元素。您的a
链接不在同一父链接下,因为每个链接都包含在h3
中。所以我相信这就是您想要的
演示 http://jsfiddle.net/gaby/NSvQB/
The issue is that
siblings
works for elements under the same parent. Youra
links are not under the same parent since each one is wrapped in ah3
.So I believe this is what you want
Demo at http://jsfiddle.net/gaby/NSvQB/
该脚本中有两个错误:
每个
元素都是
元素的唯一子元素。没有其他
兄弟姐妹。您想要的是找到手风琴标题内的所有其他
元素:
Setting the style on an element (
$(this).css(... );
) 将覆盖任何其他样式定义。稍后通过更改类来更改背景(在本例中,删除类active
)不会产生任何效果[演示]。因此,您应该设置active
类,而不是直接设置样式:Working 演示
更新:您还可以通过将所有内容放入
h3
点击事件处理程序中来简化整个代码,甚至添加active< /code> 类。
JavaScript:
CSS(更改部分):
DEMO 2
There are two errors in the script:
Each
<a>
element is the only child an<h3>
element. There are no other<a>
siblings. What you want is to find all other<a>
element inside the headers of the accordion:Setting the style on an element (
$(this).css(...);
) will overwrite any other style definitions. Changes to the background later via changing a class (in this case, removing the classactive
) will not have any effect [demo]. So instead of setting the style directly, you should set theactive
class:Working DEMO
Update: You could also simplify the whole code by putting everything into the
h3
click event handler and even add theactive
class to it.JavaScript:
CSS (changed part):
DEMO 2