我正在使用 Code Mirror 构建一个代码游乐场。我的问题是我无法从我拥有的用于 html、css 和 js 的 textarea 元素中获取内容。除了这些 textarea 元素之外,我还创建了一个 textarea,以查看我从 textarea 获取内容的代码是否有问题。所以在我的 html 中,我当然有 textarea 标签。我给这个新的文本区域一个类名“txtb”,并为一个名为“AlertContent()”的函数创建了一个带有 onclick 属性的链接。这是该函数的javascript:
function AlertContent(){
alert(document.getElementsByClassName('txtb')[0].value);
}
猜猜看,它有效。当我键入具有类“txtb”的 textarea 元素并单击调用我创建的函数 AlertContent() 的链接时,我看到一个消息框出现,显示我在该 textarea 中键入的文本。有用。但是,当我尝试为与代码镜像一起使用的 textarea 元素提供类,并使用相同的函数从这些 textarea 元素中获取值或内容时,即使有文本(html , css, js) 在那些 textarea 元素中。我想做的就是创建一个函数来获取 textarea 元素(html、css、js)的内容,这样我就可以将它保存到我的数据库中。我非常感谢您的帮助。这是我用于代码镜像的代码。
<!-- Code Editors -->
<section id="code_editors">
<div id="html" class="code_box">
<h3>HTML</h3>
<form><textarea class="MyHtml" name="htmlcode"></textarea></form>
</div>
<div id="css" class="code_box">
<h3>CSS</h3>
<textarea class="MyCss" name="css"></textarea>
</div>
<div id="js" class="code_box">
<h3>JavaScript</h3>
<textarea class="MyJs" name="js"></textarea>
</div>
</section>
</div>
<script src="http://codemirror.net/lib/codemirror.js"></script>
<!-- For HTML/XML -->
<script src="http://codemirror.net/mode/xml/xml.js"></script>
<script src="http://codemirror.net/mode/htmlmixed/htmlmixed.js"></script>
<!-- For CSS -->
<script src="http://codemirror.net/mode/css/css.js"></script>
<!-- For JS -->
<script src="http://codemirror.net/mode/javascript/javascript.js"></script>
</div>
</div>
<script>
(function() {
// Base template
var base_tpl =
"<!doctype html>\n" +
"<html>\n\t" +
"<head>\n\t\t" +
"<meta charset=\"utf-8\">\n\t\t" +
"<title>Test</title>\n\n\t\t\n\t" +
"</head>\n\t" +
"<body>\n\t\n\t" +
"</body>\n" +
"</html>";
var prepareSource = function() {
var html = html_editor.getValue(),
css = css_editor.getValue(),
js = js_editor.getValue(),
src = '';
// HTML
src = base_tpl.replace('</body>', html + '</body>');
// CSS
css = '<style>' + css + '</style>';
src = src.replace('</head>', css + '</head>');
// Javascript
js = '<script>' + js + '<\/script>';
src = src.replace('</body>', js + '</body>');
return src;
};
var render = function() {
var source = prepareSource();
var iframe = document.querySelector('#output iframe'),
iframe_doc = iframe.contentDocument;
iframe_doc.open();
iframe_doc.write(source);
iframe_doc.close();
};
// EDITORS
// CM OPTIONS
var cm_opt = {
mode: 'text/html',
gutter: true,
lineNumbers: true,
};
// HTML EDITOR
var html_box = document.querySelector('#html textarea');
var html_editor = CodeMirror.fromTextArea(html_box, cm_opt);
html_editor.on('change', function (inst, changes) {
render();
});
// CSS EDITOR
cm_opt.mode = 'css';
var css_box = document.querySelector('#css textarea');
var css_editor = CodeMirror.fromTextArea(css_box, cm_opt);
css_editor.on('change', function (inst, changes) {
render();
});
// JAVASCRIPT EDITOR
cm_opt.mode = 'javascript';
var js_box = document.querySelector('#js textarea');
var js_editor = CodeMirror.fromTextArea(js_box, cm_opt);
js_editor.on('change', function (inst, changes) {
render();
});
// SETTING CODE EDITORS INITIAL CONTENT
html_editor.setValue('<p>Empty Room</p>');
css_editor.setValue('body { color: gray; }');
// RENDER CALL ON PAGE LOAD
// NOT NEEDED ANYMORE, SINCE WE RELY
// ON CODEMIRROR'S onChange OPTION THAT GETS
// TRIGGERED ON setValue
// render();
// NOT SO IMPORTANT - IF YOU NEED TO DO THIS
// THEN THIS SHOULD GO TO CSS
/*
Fixing the Height of CodeMirror.
You might want to do this in CSS instead
of JS and override the styles from the main
codemirror.css
*/
var cms = document.querySelectorAll('.CodeMirror');
for (var i = 0; i < cms.length; i++) {
cms[i].style.position = 'absolute';
cms[i].style.top = '30px';
cms[i].style.bottom = '0';
cms[i].style.left = '0';
cms[i].style.right = '0';
cms[i].style.height = '100%';
}
/*cms = document.querySelectorAll('.CodeMirror-scroll');
for (i = 0; i < cms.length; i++) {
cms[i].style.height = '100%';
}*/
}());
// Basically adding to the CM Options
cm_opt['extraKeys'] = {
Tab: function(instance) {
if (inst.somethingSelected())
CodeMirror.commands.indentMore(inst);
else
CodeMirror.commands.insertTab(inst);
}
}
</script>