这是一个 JSBin:http: //jsbin.com/ofusec/2/
当您打开 DIV(自定义或个性化)时,该函数会正常运行,截断“+”并替换为“-”符号,但是当用户关闭 DIV 时,字符串会被复制并放入。
我希望结尾的行为与开头相同(截断并替换最后一个字符)
这是代码:
HTML:
<div id="customize" class="leftDiv">
<span class="smallTitle" onclick="toggle_expand('customizePort');">Customize Portfolio +</span><br />
<div class="leftDivContent" id="customizePort">
LAYOUT:<BR />
NAVIGATION:<BR />
BACKGROUND COLOR:<BR />
FONT COLOR:<BR />
</div>
</div>
<div id="personalize" class="leftDiv">
<span class="smallTitle" onclick="toggle_expand('personalizePort');">Personalize Portfolio +</span><br />
<div class="leftDivContent" id="personalizePort">
HEADER IMAGE:<BR />
ARTIST STATEMENT:<br />
CONTACT INFORMATION:<br />
</div>
</div>
JAVASCRIPT:
function toggle_expand(id){
var e = document.getElementById(id);
var text = $(e).siblings().text();
if( $(e).css('display') === 'none' ){
$(e).slideToggle('fast', function(){
var newText = text.replace("+","-");
$(e).siblings().text( newText );
});
} else {
$(e).slideToggle('fast', function(){
var newText = text.replace("-","+");
$(e).siblings().text( newText );
});
}
}