[英]How to add plus minus symbol to a basic accordion using jquery?
I'm using basic jQuery accordion written by Luca Filosofi from this thread . 我正在使用Luca Filosofi从该线程编写的基本jQuery手风琴。 Here is my version:
这是我的版本:
$('.product-details .post-body dl dt').on('click', function (e) {
e.preventDefault();
$(this).parent('dl').children('dd:visible').slideUp('fast');
$(this).nextUntil('dt').filter(':not(:visible)').slideDown('fast');
});
The script works perfectly. 该脚本可以完美运行。 However, I don't know how to have plus and minus text (
+
, -
) attached before the Title (eg: + Title) based if the content is shown or not. 但是,基于内容是否显示,我不知道如何在标题 (例如:+标题)之前附加正负文本(
+
, -
)。
I have the following permanent markup and it must stay as is. 我有以下永久标记,它必须保持原样。 Must not add more class or html, except via jQuery:
不得添加更多的类或HTML,除非通过jQuery:
<dl>
<dt>Title</dt>
<dd>Description 01</dd>
<dd>Description 02</dd>
<dt>Title</dt>
<dd>Description 01</dd>
<dd>Description 02</dd>
<dd>Description 03</dd>
</dl>
You could toggle the icons when they click like this: 您可以在图标单击时切换图标,如下所示:
$('.product-details .post-body dl dt').on('click', function (e) {
e.preventDefault();
$(this).parent('dl').children('dd:visible').slideUp('fast');
$(this).nextUntil('dt').filter(':not(:visible)').slideDown('fast');
$(this).closest('i').toggleClass("icon-circle-plus icon-circle-minus");
});
You need to get last character of dt
text using .slice()
. 您需要使用
.slice()
获取dt
文本的最后一个字符。 If last character is +
change it to -
and if it is -
change to +
. 如果最后一个字符是
+
将其更改为-
如果它是-
更改为+
。
$('dl dd').slideUp('fast'); $('dl dt').on('click', function () { $(this).parent().children('dd:visible').slideUp('fast'); $(this).nextUntil('dt').not(':visible').slideDown('fast'); $(this).text(function(i, text){ return (text.slice(-1) == "-") ? (text.slice(0, -1) + " +") : (text.slice(0, -1) + " -"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <dl> <dt>Title +</dt> <dd>Description 01</dd> <dd>Description 02</dd> <dt>Title +</dt> <dd>Description 01</dd> <dd>Description 02</dd> <dd>Description 03</dd> </dl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.