使用 jQuery 切换文本

Toggling a text with jQuery

本文关键字:文本 jQuery 使用      更新时间:2023-09-26

如何在 li 开头的 "+" 或 "-" 之间切换 .text() (单击时)?它适用于 http://flatfilthy.se/dominic/最底部的扩展对象

$(function(){
   $(".vaccordian li.expander").on('click', function(){
      $(this).toggleClass('active').siblings().removeClass('active');
   });
});

你可以用一些CSS来实现这一点,例如:

li.expander:before {
    content: '+';
}
li.expander.active:before {
    content: '-';
}
$(".vaccordian li.expander").on('click', function(){
    $(this).toggleClass('active').siblings().removeClass('active');
    $(this).text(function(oldtext) {
        var first = oldtext.substr(0, 1);
        return (first == "+" ? "-" : "+") + oldtext.substr(1);
    });
});