-4

我需要在切换时将 + 更改为 - 。代码设置在 http://jsfiddle.net/B65Ht/

HTML

<a class="expandB" href="#">
   <h3>Advocacy + </h3>
</a>
<div class="content">
    Content goes here
</div>

jQuery

$(document).ready(function(){
    $('.content').hide();
    $("a.expandB").click(function() {  
        $(this).toggleClass("expandB").next().slideToggle("slow", function(){

       // script to toggle between + and -
     });

return false; //Prevent the browser jump to the link anchor/Prevent the browser jump to the link anchor
});


});​

​</p>

4

4 回答 4

3

jsFiddle

你可以这样做:

 $("a.expandB").click(function() {

        var $a = $(this);            
        var $h3 = $a.find('h3');             
        $(this).toggleClass("expandB").next().slideToggle("slow", function() {
             var text = $h3.text();
             $a.hasClass('expandB') ? $h3.text(text.replace('-', '+')) : $h3.text(text.replace('+', '-')); 
        });
        // Toggles the text from expand to Collapse
       return false; //Prevent the browser jump to the link anchor
    });
于 2012-07-06T15:24:45.160 回答
0

您的问题与此问题非常相似,因此您的答案也可能相似:

jQuery .click() .toggle() - 改变显示和交换字符

于 2012-07-06T15:26:00.833 回答
0

不需要额外的课程。只需通过交换.text()

$("a.expandB").click(function() {  

    $('span', this).text( $('span').text() == '+' ? '-' : '+');

    $(this).toggleClass("expandB").next().slideToggle("slow");    

    return false;

});

http://jsfiddle.net/iambriansreed/aZaBe/3/

于 2012-07-06T15:26:25.863 回答
0
$("a.expandB").click(function() {  
    var t=$(this);
    t.toggleClass("expandB").next()
    .slideToggle("slow", function(){
        if(t.hasClass('expandB')) $('h3', t).html('Advocacy +'); 
        else $('h3', t).html('Advocacy -'); 
    });
    return false;
});

演示。

于 2012-07-06T15:33:03.177 回答