一个良好的开端
我问这个问题“Codemirror - Use on multiple textareas?”前几年有一个很好的答案。但是,它仍然需要一个ID为参数。 ID是唯一的。
查找类文本区域,而不是ID
有多个文字区域,一些HTML和一些与在同一页上的CSS时,将是很好的补充,而不是一个ID类。
例
<p>Some content</p>
<textarea class="my_codemirror_html">
</textarea>
<p>Some content</p>
<textarea class="my_codemirror_html">
</textarea>'
如果我可以使用jQuery它,它的罚款。我使用反正在页面上。
更新2012-02-21 - 几乎有
我发现jsFiddle这个职位。唯一缺少的是,我不能让它在文字区域工作。
这将是更低的复杂的简单的解决方案。
$('.my_codemirror_html').each(function(index, elem){
CodeMirror.fromTextArea(elem, {/*options*/});
});
我通过添加与jQuery的ID的所有文本域解决了这个问题。
jQuery(document).ready(function($) {
var code_type = '';
$('.code-html').each(function(index) {
$(this).attr('id', 'code-' + index);
CodeMirror.fromTextArea(document.getElementById('code-' + index), {
mode: "text/html",
lineNumbers: true,
tabMode: "indent"
}
);
});
});
所有的JavaScript版本:
var codemirrorInstance = [];
var foundtextareasarr = document.getElementsByClassName('classForTextareas');
for(var i = 0; foundtextareasarr[i]; ++i) {
codemirrorInstance[i] = CodeMirror.fromTextArea(foundtextareasarr[i], {
lineNumbers: true,
mode: "add-your-mode-here"
});
}
上面的代码将通过一流的找到所有文字区域,并将其转换为只使用JavaScript和jQuery的不是个别codemirror实例。