Javascript 用Javascript删除字符串的最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1990265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:17:07  来源:igfitidea点击:

Delete Last Char of String with Javascript

javascriptjquerystring

提问by Mikkel

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

我有DIV一些字符。每次单击文本DIV本身时,如何从文本中删除最后一个字符?

回答by Sampson

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/

查看每个在线示例:http: //jsfiddle.net/Xcn6s/

回答by Tim Down

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 的引用并且它只包含一个文本节点(正如您的问题所暗示的那样),这非常简单。这是一个非 jQuery 解决方案:

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

回答by Dan Beam

Edit: here's the easiest way to do this without any library dependencies

编辑:这是在没有任何库依赖项的情况下执行此操作的最简单方法

function removeLastChar(node) {
    var i = node.childNodes.length;
    while (--i >= 0) {
        if (3 === node.childNodes[i].nodeType) {
            node.childNodes[i].data = node.childNodes[i].data.replace(/\S\s*$/, '');
            break;
        }
    }
}

/\S\s*$/stillmeans "the last non-space at the end"

/\S\s*$/仍然表示“最后的最后一个非空格”

Note: borrowed from Tim Down's solution and further years of web development experience, :)

注意:借鉴了 Tim Down 的解决方案和多年的 Web 开发经验,:)

回答by Tatu Ulmanen

Alternative to Jonathan's answer, how to delete the first character:

替代乔纳森的回答,如何删除第一个字符:

$("div.myDiv").click(function(){
    $(this).html($(this).text().substring(1));
});

Or, remove the last character:

或者,删除最后一个字符:

$("div.myDiv").click(function(){
    $(this).html($(this).text().replace(/.$/g, ''));
});

Or, go crazy and remove a character from a random place:

或者,发疯并从随机位置删除一个角色:

$("div.myDiv").click(function(){
    var text = $(this).text();
    var index = Math.round(Math.random() * (text.length - 1));
    var result = text.substring(0, index) + text.substring(index + 1, text.length - 1);
    $(this).html(result);
});

Instead of random place, you can use the above function with a predefined index to remove from a specific location.

您可以使用带有预定义索引的上述函数从特定位置删除,而不是随机位置。

回答by Jeff B

This deletes a character each time you click on the div. Without more requirements, I can't give you any more.

每次单击 div 时都会删除一个字符。没有更多的要求,我不能给你更多。

<html>

<head>
    <script>

    function deleteChar(div) {
       div.innerHTML = div.innerHTML.replace(/.$/, '');
    }

    </script>
</head>

<body>

    <div onclick="deleteChar(this)">this is my div</div>

</body>
</html>

Oh, sorry, you asked for jquery... Well, this is how you do it in straight javascript.

哦,对不起,你要求 jquery ......好吧,这就是你在直接 javascript 中的做法。

回答by mopoke

If you wanted to delete the first character of the text every click you could use substring. e.g.:

如果您想在每次点击时删除文本的第一个字符,您可以使用 substring。例如:

$("#textDiv").click(function(){
  $(this).html($(this).text().substring(1));
});

回答by Paul Creasey

To supplemnt on Jonathons answer, use..

要补充乔纳森的回答,请使用..

$("div.myDiv").click(function(){
  $(this).html( $(this).text().replace("/.$/gi", "o") );
});

To remove the last character

删除最后一个字符

回答by Paul Creasey

Plain JavaScript:

纯 JavaScript:

<div id="text">This is some text</div>
<script>
var node = text.firstChild;
var nodeValue = "";
var nodeValueLength = 0;
text.onclick = function () {
    nodeValue = node.nodeValue;
    node.nodeValue = nodeValue.substring(0, nodeValue.length - 1);
};
</script>

回答by Ashish

Its easy by using the attribute$= CSS3 selector which means "ends with" in your jQuery selection:

通过使用 attribute$= CSS3 选择器很容易,这意味着在您的 jQuery 选择中“以”结尾:

To Delete 1st character:

删除第一个字符:

$("div[id$=/]").each(function(){ 
    this.id = this.id.substr(1);
});

To Delete Last Character:

删除最后一个字符:

$("div[id$=/]").each(function(){ 
    this.id = this.id.substr(0,this.id.length - 1);
});