简体   繁体   English

如何在javascript中反转字符串?

[英]How to reverse a string in javascript?

I'm trying to learn JavaScript through.html files in Visual Studio 2013.我正在尝试通过 Visual Studio 2013 中的 .html 文件学习 JavaScript。

I know there are easier ways to reverse a string in JavaScript, but my main aim is to understand interaction between html and JavaScript to provide such a solution.我知道有更简单的方法可以在 JavaScript 中反转字符串,但我的主要目的是了解 html 和 JavaScript 之间的交互以提供这样的解决方案。

Also, the IDE has to be Visual Studio as eventually I want to code this same logic using.aspx files.此外,IDE 必须是 Visual Studio,因为最终我想使用 .aspx 文件编写相同的逻辑。

So, for my initial learning attempt, I'm trying to implement the following logic:因此,对于我最初的学习尝试,我试图实现以下逻辑:

  • get a string from input tag of form,从表单的输入标签中获取一个字符串,
  • submit form resulting in the script submit() to run,提交表单导致脚本submit()运行,
  • the script reverses the string, and then脚本反转字符串,然后
  • displays reversed string in the same input tag which is now disabled在现在已禁用的同一输入标签中显示反转字符串

Code I've written is as follows:我写的代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <script type="text/javascript">
        function reverseString() {
            var s = document.getElementById('reverseString').value;
            var reversed;

            for (var i = s.length - 1; i >= 0; i--) {
                reversed += s[i];
            }

            document.getElementById('reverseString').disabled = true;
            document.getElementById('reverseString').value = reversed;
        }

        function submit(form) {
            reverseString();
        }
    </script>

    <title>Play with Code</title>
</head>

<body>
    <form name="myform" onsubmit="submit();">
        Reverse String: <input type="text" id="reverseString"/><br/>
        <input type="submit" value="Submit"/>
    </form> 
</body>
</html>

It doesn't work and I'm really clueless as to how to solve it.它不起作用,我真的不知道如何解决它。

I unsuccessfully tried to debug following the instructions provided by these links:我尝试按照这些链接提供的说明进行调试但未成功:

How to debug (only) JavaScript in Visual Studio? 如何在 Visual Studio 中调试(仅)JavaScript?

http://www.aspsnippets.com/Articles/Debug-JavaScript-and-jQuery-using-Visual-Studio-in-Internet-Explorer-browser.aspx http://www.aspsnippets.com/Articles/Debug-JavaScript-and-jQuery-using-Visual-Studio-in-Internet-Explorer-browser.aspx

Please help me to fix this code and also, if possible, please advise or direct me on how to debug such a piece of code.请帮我修复这段代码,如果可能的话,请建议或指导我如何调试这样一段代码。

Thanks.谢谢。

Here's a working code with comments to explain you what you are doing wrong:这是一个带有注释的工作代码,可以解释您做错了什么:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <title>Play with Code</title>
</head>
<body>
    <form name="myform" id="form">
        Reverse String: <input type="text" id="reverseString"/><br/>
        <input type="submit" value="Submit"/>
    </form>
    <script>
        function reverseString() {
            var s = document.getElementById('reverseString').value;
            // reverse should initialized as a empty String
            // to prevent adding char to "undefined" string
            var reversed = '';

            for (var i = s.length - 1; i >= 0; i--) {
                reversed += s[i];
            }

            document.getElementById('reverseString').disabled = true;
            document.getElementById('reverseString').value = reversed;
        }

        function submit(ev) {
            // preventDefault prevent the form to do his automatic
            // behavior which submit the form with a new HTTP request
            ev.preventDefault();
            reverseString();
        }

        // Attach the event to the form
        document.getElementById('form').addEventListener('submit', submit);
    </script>
</body>
</html>

Working Demo工作演示

More Info on Debugging 有关调试的更多信息

//HTML
Reverse String: <input type="text" id="reverseString"/><br/>
    <input type="button" value="Submit" onClick="funcreverse()"/>

//Script
 function funcreverse () {
        var s = document.getElementById('reverseString').value;
        var reversed = '';
        for (var i = s.length - 1; i >= 0; i--) {
            reversed += s[i];
        }

        document.getElementById('reverseString').disabled = true;
        document.getElementById('reverseString').value = reversed;
    };
"use strict";
function reverseString(str) {
  let rev = "";
  let len = str.length;
  for (let i = len - 1; i >= 0; i--) {
    rev += str[i];
  }
  return rev;
}
console.log(reverseString("Reverse"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM