我能够替换第一次出现的,,但现在我想删除最后一次出现的。

    $(function() {
    $(".defaultUL li").each(function() {
        var text = $(this).html();
        if(text.indexOf("...")<0) {
        var s = text.lastIndexOf(",");
        $(this).html(text.replace(s," "));
        }
    });
});


","代替s不能按我希望的那样工作。

最佳答案

不是replace的第一个参数应该是您要替换的字符串,而不是您要进行替换的索引吗?

尝试

$(this).html(text.substring(0, s) + " " + text.substring(s+1));

关于javascript - 帮助用新字符替换最后出现的字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5172530/

10-12 12:45
查看更多