Delete Last Char of String with Javascript

前端 未结 9 726
独厮守ぢ
独厮守ぢ 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:09

    Assuming you have a reference to the div and it just contains a single text node (as your question implies), this is very simple. Here's a non-jQuery solution:

    div.onclick = function() {
        var textNode = this.firstChild;
        textNode.data = textNode.data.slice(0, -1);
    };
    

提交回复
热议问题