我再次在这里再次提出同样的问题。

现在我的老问题解决了。我有两个文件index.html和code.js
1)index.html

<html>
<head>
<title> perl </title>
<link rel="stylesheet" href="http://code.guru99.com/css/1140.css" type="text/css" media="screen" />
        <link rel="stylesheet" href="http://code.guru99.com/css/styles.css" type="text/css" media="screen" />
        <script src="http://code.guru99.com/php/lib/codemirror.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">></script>
        <link rel="stylesheet" href="http://code.guru99.com/Sanjay/lib/codemirror.css" type="text/css" media="screen" />
        <script src="code.js"></script>
        <script type="text/javascript" src="http://code.guru99.com/perl/perl.js"></script>
<style>
 .CodeMirror {
    border: 1px solid #eee;
    height: auto;
    width : 600px;
}
.CodeMirror-scroll {
    height: auto;
    overflow-y: hidden;
    overflow-x: auto;
}
</style>
</head>
<body>
Integer : whole numbers e.g. -3, 0, 69. The maximum value of an integer is platform-dependent. On a 32 bit machine, its usually around 2 billion. 64 bit machines usually have larger values. The constant PHP_INT_MAX is used to determine the maximum value.
<pre class="codeguru">say 'hi';</pre>
Let us now look at how PHP determines the data type depending on the attributes of the supplied data.
<pre class="codeguru">say 'hello';</pre>
Floating point numbers
<pre class="codeguru">say 'you r amazing';</pre>
Character strings
<pre class="codeguru">say 'i am fine';</pre>
</div>
<form class="hidden code-box" method="GET" name="sample">
<div dir="ltr"><textarea class="php" name="codeguru"></textarea></div>
<input type="button" value="Run" />
</br></br>
Output:</br></br>
    <textarea id="print-result" disabled="true" cols="77"></textarea></br>
</form></div>
</body>
</html>


和code.js包含以下代码

$(document).ready(function()
{
    $('pre.codeguru').each(function()
    {
            var pre = this;
            var form = $('form[name=sample]').clone();
            $(form).removeAttr('name');
            $(form).removeClass('hidden');
            $($(form).find('textarea')[0]).val($(pre).text());
            var id = $(pre).attr('id');
            $(form).find('div textarea[name=code]').first().attr('id', id);
        $(pre).replaceWith(form);
    });


    var n = 0;
    $('input[type=button]').each(function () {
            $(this).click(function (x) {
                return function () {
                    execute(x);
                };
            }(n++))
        }
    );

    window.editors = [];
    $('textarea[name=codeguru]').each(function()
    {
        window.editor = CodeMirror.fromTextArea(this,
        {
            lineNumbers: true,
            matchBrackets: true,
            mode: "application/x-httpd-perl",
            tabMode: "shift"
         });
            editors.push(editor);
    });



});
function execute(idx) {
      p5pkg.CORE.print = function(List__) {
          var i;
          for (i = 0; i < List__.length; i++) {
            document.getElementById('print-result').value+=p5str(List__[i])
          }
          return true;
      };
      p5pkg["main"]["v_^O"] = "browser";
      p5pkg["main"]["Hash_INC"]["Perlito5/strict.pm"] = "Perlito5/strict.pm";
      p5pkg["main"]["Hash_INC"]["Perlito5/warnings.pm"] = "Perlito5/warnings.pm";

      var source = editors[idx].getValue();
      alert(source);

      var pos = 0;
      var ast;
      var match;
      document.getElementById('print-result').value = "";
      try {
            var start = new Date().getTime();
            var js_source = p5pkg["Perlito5"].compile_p5_to_js([source]);
            var end = new Date().getTime();
            var time = end - start;
            // run
            start = new Date().getTime();
            eval(js_source);
            end = new Date().getTime();
            time = end - start;
       }
       catch(err) {
            //document.getElementById('log-result').value += "Error:\n";
       }
  }


当我运行此代码时,它的工作正常,也可以给我输出。但是我的问题是输出仅在一个文本区域中打印。我想在连续的textarea中打印输出。

code.js中的此代码输出输出。

p5pkg.CORE.print = function(List__) {
    var i;
    for (i = 0; i < List__.length; i++) {
       document.getElementById('print-result').value += p5str(List__[i])
    }
}


所以我要做的是在连续的textarea中打印输出。请帮助我。

最佳答案

对页面中的不同元素使用相同的ID可能会造成问题。
使文本区域像这样..

<textarea id="print-result" class='print-result' disabled="true" cols="77></textarea>


在code.js中打印输出的修改后的代码是

       p5pkg.CORE.print = function(List__) {
            var i,concat='';
            for (i = 0; i < List__.length; i++) {
            concat=$(".print-result").eq(idx).val();
            $(".print-result").eq(idx).val(concat+p5str(List__[i]));

       }
            return true;
        }

09-25 17:03