切换时我需要将+更改为 - 。代码设置为 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
});
});
答案 0 :(得分:3)
你可以这样做:
$("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
});
答案 1 :(得分:0)
您的问题与此问题非常相似,因此您的回答可能也类似:
jQuery .click() .toggle() - change display and swap character
答案 2 :(得分:0)
无需额外课程。只需通过.text()
进行交换;
$("a.expandB").click(function() {
$('span', this).text( $('span').text() == '+' ? '-' : '+');
$(this).toggleClass("expandB").next().slideToggle("slow");
return false;
});
答案 3 :(得分: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;
});