Var is not defined but why?
I have a little problem with Codemirror (http://codemirror.net/manual.html)
My code:
$(document).ready(function ust()
{
pa_textareas();
});
function pa_textareas()
{
var textarea = document.getElementById('ta_1');
var editor_1 = new MirrorFrame(CodeMirror.replace(textarea), {
height: "100%",
width: "100%",
parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
stylesheet: "js/js_parser/jscolors.css",
path: "js/js_parser/",
autoMatchParens: false,
content: 'test kjskljsklj skjs lkj slkj sl',
initCallback: function(getContents) { e1_ct = ''+editor_1.mirror.getCode()+''; },
onChange: function (n) { e1_ct = ''+editor_1.mirror.getCode()+''; }
});
var textarea = document.getElementById('ta_2');
var editor_2 = new MirrorFrame(CodeMirror.replace(textarea), {
height: "100%",
width: "100%",
parserfile: ["tokenizejavascript.js", "parsecss.js"],
stylesheet: "js/js_parser/csscolors.css",
path: "js/js_parser/",
autoMatchParens: false,
content: 'blub kasjdkljas dkjas lkdj alskj dlk',
initCallback: function(getContents) { e2_ct = ''+editor_2.mirror.getCode()+''; },
onChange: functio开发者_如何学运维n (n) { e2_ct = ''+editor_2.mirror.getCode()+''; }
});
}
I want to insert a button which formats the code, but I get always an error.
Button:
onclick="editor_2.mirror.reindent();"
Result:
-> "editor_2 is not defined"The scope of editor_2
is the function pa_textareas
. Since you are calling it using an intrinsic event handler attribute, you can't reach the variable.
You should probably be assigning the event handler with JavaScript inside the pa_textareas
function.
Since you are using jQuery (At least I assume you are, there are other libraries with the stupid $
variable):
jQuery('some selector').click(function () { editor_2.mirror.reindent(); });
Since the anonymous function is declared inside pa_textareas
it will have access to variables defined in that scope.
Because there is no point in your code where you assign a value to editor_code_2, but you try to use it twice.
I cannot find anywhere in your code where you create a variable called editor_code_2
. This is why you are getting the error. It seems you have mispelled your editor_2
variable.
精彩评论