JavaScript:textarea没有显示输出

时间:2017-12-10 11:35:39

标签: javascript html

我正在制作Leetspeak转换器程序,但第二个textarea没有显示任何输出。这是我的基本代码:

    <!DOCTYPE html>
    <html>
    <body>

    <h1>Leetspeak Converter</h1>

    <script language="JavaScript">
    function convert(){
      var x = document.getElementById("myTextArea").value;
      var result='';
      for (var i = 0, len = x.length; i < len; i++) {
        if (x.charAt(i)=='A'){
          result = result + '4';
        }
      }
      document.getElementById('resultTextarea').value = result ;
    }
    </script>

    <div class="input">
    <textarea id = "myTextArea" rows = "6" cols = "80">
    </textarea>
    </div>

    <div class="push">
    <button onclick="convert">Convert</button>
    </div>

    <div class="result">
    <textarea id = "resultTextArea" rows = "6" cols = "80">
    </textarea>
    </div>

根本不会产生任何输出。我尝试过使用console.log(),但没有输出。 我也使用了调试器,但没有骰子。

3 个答案:

答案 0 :(得分:2)

您有语法错误,分为两部分,因此请更改此

<button onclick="convert">Convert</button> // this does not represent a method

用这个

<button onclick="convert()">Convert</button>

另外,这个

document.getElementById('resultTextarea').value = result ; // a small typo in id

用这个

document.getElementById('resultTextArea').value = result ;

&#13;
&#13;
function convert(){
      var x = document.getElementById("myTextArea").value;
      var result=0;
      for (var i = 0, len = x.length; i < len; i++) {
        if (x.charAt(i)=='A'){
          result = result + 4;
        }
      }
      document.getElementById('resultTextArea').value = result ;
    }
&#13;
<div class="input">
    <textarea id = "myTextArea" rows = "6" cols = "80">
    </textarea>
    </div>

    <div class="push">
    <button onclick="convert()">Convert</button>
    </div>

    <div class="result">
    <textarea id = "resultTextArea" rows = "6" cols = "80">
    </textarea>
    </div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您的语法错误:

<button onclick="convert">Convert</button>

将此修复为:

<button onclick="convert()">Convert</button>

答案 2 :(得分:1)

试试这个

<!DOCTYPE html>
    <html>
    <body>

    <h1>Leetspeak Converter</h1>

    <script language="JavaScript">
    function convert(){
      var x = document.getElementById("myTextArea").value;

      var result='';


      for (var i = 0,len = x.length ; i < len; i++) {
        if (x.charAt(i)=='A'){
          result = result + '4';
        }
      }

      document.getElementById('resultTextArea').value = result ;
    }
    </script>

    <div class="input">
    <textarea id = "myTextArea" rows = "6" cols = "80">
    </textarea>
    </div>

    <div class="push">
    <button onclick="convert()">Convert</button>
    </div>

    <div class="result">
    <textarea id = "resultTextArea" rows = "6" cols = "80">
    </textarea>
    </div>
    </body>
    </html>