Reversing a string in JavaScript

后端 未结 15 2033
野的像风
野的像风 2020-11-28 03:58

I\'m trying to reverse an input string

var oneway = document.getElementById(\'input_field\').value();
var backway = oneway.reverse();

but f

15条回答
  •  盖世英雄少女心
    2020-11-28 04:47

    I think you'll find that in fact reverse() isn't a function in jQuery. Incidentally, jQuery is really good at manipulating your DOM, but isn't really for string manipulation as such (although you can probably get plugins/write your own) to do this.

    The best way I've found to reverse a string in javascript is to do the following:

    String.prototype.reverse = function(){
    splitext = this.split("");
    revertext = splitext.reverse();
    reversed = revertext.join("");
    return reversed;
    }
    

    Found at: http://www.bytemycode.com/snippets/snippet/400/

    I think you'll find that if you pop the above into your code somewhere, your call to .reverse() should work :)

提交回复
热议问题