Delete Last Char of String with Javascript

前端 未结 9 898
独厮守ぢ
独厮守ぢ 2021-02-07 05:57

I have a DIV with some characters. How can I remove the last character from the text with each click on the DIV itself?

9条回答
  •  佛祖请我去吃肉
    2021-02-07 06:00

    Removing First Character

    ​$("div").on("click", function(){
        $(this).text(function(index, text){
            return text.replace(/^.(\s+)?/, '');
        });
    });​​​​​​​​​​​​
    

    Removing Last Character

    $("div").on("click", function(){
        $(this).text(function(index, text){
            return text.replace(/(\s+)?.$/, '');
        });
    });
    

    Removing a Specific Char

    $("div").on("click", function(){
        $(this).text(function(index, text){
            return text.replace(/r/gi, '');
        });
    });
    

    See an example of each online at: http://jsfiddle.net/Xcn6s/

提交回复
热议问题