交换图标以显示相关的展开或折叠
Swap icon to show relevant expand or collapse
我有一些代码可以解释和折叠内容。这段代码最初用扩展/折叠(在下面注释)代替了标题,但我希望在唯一的h2标记旁边有一个相关的图像或文本(+/-)。
脚本是:
$(".header").click(function () {
$header = $(this);
//getting the next element
$content = $header.next();
//open up the content needed
$content.slideToggle(500, function () {
//execute this after slideToggle is done
//change text of header based on visibility of content div
//$header.text(function () {
//change text based on condition
//return $content.is(":visible") ? "-" : "+";
//});
});
});
内容看起来像
<div class="header">
<h2>The Role</h2>
</div>
<div class="content">
<?php the_field('the_role')?>
</div>
<div class="header">
<h2>Your Experience</h2>
</div>
<div class="content">
<?php the_field('your_experience')?>
</div>
希望这是有道理的。
如果我理解你的问题,我想这就是你想要的:
http://jsfiddle.net/wf_4/NJR22/
HTML:
<div class="header">
<span>-</span>
<h2>The Role</h2>
</div>
<div class="content">
text here
</div>
<div class="header">
<span>-</span>
<h2>Your Experience</h2>
</div>
<div class="content">
text here
</div>
脚本:
$(".header").click(function () {
var $header = $(this),
$span = $header.find(">:first-child"),
//getting the next element
$content = $header.next();
//open up the content needed
$content.slideToggle(500)
if ($span.text() == "-")
$span.text("+")
else {
$span.text("-")
}
});
CSS:
.header > span {
float:left;
width:20px
}
修复您当前的问题。这将工作
$(function() {
$('.header').each(function() {
if( $(this).next('div.content').length > 0 ) {
$(this).prepend('<img src="collapsed.png" class="collapser" />');
}
});
});
$(".header").click(function () {
$header = $(this);
//getting the next element
$content = $header.next();
//open up the content needed
$content.slideToggle(500, function () {
//execute this after slideToggle is done
//change text of header based on visibility of content div
//$header.text(function () {
//change text based on condition
if( $content.is(":visible") ) {
$header.find('.collapser').attr('src','collapsed.png');
} else {
$header.find('.collapser').attr('src','expanded.png');
}
//return $content.is(":visible") ? "-" : "+";
//});
});
});
如果我是你,我会重构,并这样做HTML
<div class="accordio">
<div class="header">
<img src="collapsed.png" class="collapser" />
<h2>The Role</h2>
</div>
<div class="content">
ss
</div>
</div>
<div class="accordio">
<div class="header">
<img src="collapsed.png" class="collapser" />
<h2>Your experience</h2>
</div>
<div class="content">
ss
</div>
</div>
脚本
$(".accordio .header").click(function () {
$header = $(this);
$content = $header.parent().find('.content');
$content.slideToggle(500, function () {
if( $content.is(":visible") ) {
$header.find('.collapser').attr('src','collapsed.png');
} else {
$header.find('.collapser').attr('src','expanded.png');
}
});
});
相关文章:
- Javascript更改图标
- ExtJS——在展开/折叠时调整面板高度
- 单击更改图标并通过javascript添加一个css类
- 我希望只有在我滚动页面后才能显示我的返回页首图标
- 展开和折叠文件夹
- 展开和折叠自举手风琴
- 如何更改角度谷歌地图上的集群图标
- Twitter 引导中的图标箭头位置 - 折叠插件
- 为什么我的引导手风琴图标在展开/折叠时没有改变
- 在 jquery 移动版中分别分隔可折叠文本和图标的事件
- 更改折叠折叠时的图标方向
- 如何在Bootstrap折叠中更改图标以满足多种需求
- 更改angularjs中展开-折叠的图标
- 棱角材质:当侧边折叠时显示菜单图标
- jQuery树视图中的展开和折叠图标在树中不显示
- 用+和-图标折叠/展开表格
- 折叠/打开一个字体很棒的图标表
- 交换图标以显示相关的展开或折叠
- 一个图标上的Javascript,用于展开和折叠不使用回车按钮的表
- jQuery翻转箭头图标基于折叠位置