2016-07-25 24 views
0

我使用的是由Luca Filosofi编写的基本jQuery手风琴,来自this thread。这是我的版本:如何使用jquery将加减号添加到基本手风琴?

$('.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'); 
}); 

该脚本完美地工作。但是,我不知道如果在标题(例如:+标题)之前加上加号和减号文字(+,-),如果显示或不显示内容。

我有以下永久标记,它必须保持原样。不能添加更多的类或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> 
+0

是什么*内容含义显示或不显示。*? – Mohammad

+0

我使用多个描述来定义一个单词,将

视为触发按钮,将所有
视为与内容相同的组。点击单个
将展开其所有内容并自动折叠其他群组内容。就像垂直标签 – Biyan

回答

1

你可以切换图标,当用户点击这样的:

$('.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"); 
}); 
+0

我做了一个小小的修改,把'我'改为'dt'。 - 但它触发了一个活跃的类作为切换,而不是手风琴。它只会在单击相同的“dt”时删除活动类。 – Biyan

+0

他们发现元素有加号,把它变成减号,并将当前点击的元素改为加号。 ('click',function(e){e.preventDefault(); $(this).parent''dl')。children() '':'fast'); $(this).nextUntil('dt')。filter(':not(:visible)')。slideDown('fast'); $('。 ('。icon-circle-plus')。attr('class','icon-circle-minus'); $(this).closest('dt')。 attr('class','icon-circle-plus');});' 希望它有帮助 – CardCaptor

1

您需要获得使用.slice()dt文本的最后一个字符。如果最后一个字符是+更改为-,如果它是-更改为+

$('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>

+0

感谢您的答案。不幸的是,我们有数百篇使用给定标记发布的文章,而且还会有更多文章,所以为每篇文章定制html确实需要大量工作,而不是为将来发布提供一个好的步骤。除了通过jQuery之外,我们更好地使用默认HTML,而不添加额外的符号或html行。 – Biyan

+0

@Biyan没有问题。你可以像使用jQuery那样做[** demo **](https://jsfiddle.net/66y6udrb/) – Mohammad

+0

如何在Title之前移动“+/-”? – Biyan

相关问题