我有两个文件
- 索引.php
- 代码.js
我在 index.php 中有一些代码,其中包含表单并在 code.js 的帮助下。我正在用该表单替换 pre 标签。所以一切运行良好。要了解我的问题,您必须查看我的代码。所以首先我给出我的代码
index.php 文件:
<html>
<head>
<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" />
<link rel="stylesheet" href="http://codemirror.net/doc/docs.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 type="text/javascript" src="http://code.guru99.com/perl/perl.js"></script>
<script type="text/javascript" src="cperl.js"></script>
<script src="code.js"></script>
</head>
<body>
<style type="text/css">
.CodeMirror {
border: 1px solid #eee;
height: auto;
width : 630px;
}
.CodeMirror-scroll {
height: auto;
overflow-y: hidden;
overflow-x: auto;
}
</style>
<p>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.</p>
<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="codeNrun">say 'i am fine';</pre>
<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-result4" disabled="true" cols="77"></textarea>
<br/>
</form>
<form class="hidden code-box" method="GET" name="Nosample">
<div dir="ltr">
<textarea class="php" name="codeNrun"></textarea>
</div>
</form>
</body>
</html>
code.js 文件:
$(document).ready(function () {
var idIndex = 0;
$('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);
$(form).find('#print-result4').first().attr('id', 'print-result' + idIndex++);
$(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: "perl",
tabMode: "shift"
});
editors.push(editor);
});
$('pre.codeNrun').each(function () {
var pre = this;
var form = $('form[name=Nosample]').clone();
$(form).removeAttr('name');
$(form).removeClass('hidden');
$($(form).find('textarea')[0]).val($(pre).text());
var id = $(pre).attr('id');
$(form).find('div textarea[name=codeNrun]').first().attr('id', id);
$(pre).replaceWith(form);
});
window.editors = [];
$('textarea[name=codeNrun]').each(function () {
window.editor = CodeMirror.fromTextArea(this, {
lineNumbers: true,
matchBrackets: true,
mode: "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' + idx).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();
var pos = 0;
var ast;
var match;
document.getElementById('print-result' + idx).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";
}
}
所以现在你可以看到有两个表单名称 sample 和 Nosample 像这样
<form class="hidden code-box" method="GET" name="sample">
和
<form class="hidden code-box" method="GET" name="Nosample">
并且有两种类型的 pre 标签 codeguru 和 codeNrun 像这样
<pre class="codeguru">say 'you r amazing';</pre>
和
<pre class="codeNrun">say 'i am fine';</pre>
因此,当我用表单替换此预代码时,当我尝试运行时,输出将打印在另一个文本区域中,我无法理解为什么会发生这种情况。