简体   繁体   English

使用jQuery切换文本

[英]Toggling a text with jQuery

How can I toggle a .text() between "+" or "-" at the start of the li (when clicked) ? 如何在li的开头(单击时 )在“ +”或“-”之间切换.text( It's for the expanding object at the very bottom of http://flatfilthy.se/dominic/ 用于http://flatfilthy.se/dominic/最底部的扩展对象

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

You could accomplish this with a bit of CSS, for example: 您可以使用一些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);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM