我试图调用一个我知道存在于变量上的函数,但由于某种原因,我无法从传递的函数中调用onUpdate
- 任何想法家伙 - 抱歉新手
//set up the mirror when view is expanded
function attachMirrorListener(section, mirror) {
var textarea = document.getElementById(mirror.attr('id'));
var input = $(mirror.data('input'));
var myCodeMirror;
section.click(function() {
if(section.hasClass('open') && !section.hasClass('mirror-added')) {
section.addClass('mirror-added');
input.attr('value','test value!!');
myCodeMirror = CodeMirror.fromTextArea(textarea, {
onUpdate : function(){ myCodeMirror.save(); //does not work here!!
},
});
}
console.log(myCodeMirror);
myCodeMirror.save(); //works here
});
}
});
更新
我尝试重新排列代码,仍然无法正常工作
jQuery(document).ready(function($) {
var _codeMirror;
//setup code-mirror
$('.customize-section').each(function() {
var section = $(this);
var mirror = section.find('textarea.wpec-tc-code-mirror');
if(mirror.length > 0) {
attachMirrorListener(section, mirror);
}
});
//set up the mirror when view is expanded
function attachMirrorListener(section, mirror) {
var textarea = document.getElementById(mirror.attr('id'));
var input = $(mirror.data('input'));
section.click(function() {
if(section.hasClass('open') && !section.hasClass('mirror-added')) {
section.addClass('mirror-added');
_codeMirror = CodeMirror.fromTextArea(textarea, {
onUpdate : codemirrorcallback,
});
}
});
function codemirrorcallback() {
_codeMirror.save();
input.val(escape(textarea.value));
}
}
});
这是来自 chrome 控制台的错误日志
Uncaught TypeError: Cannot call method 'save' of undefined codemirror.js:26
codemirrorcallback codemirror.js:26
endOperation codemirror.js:1864
instance.(anonymous function) codemirror.js:1871
CodeMirror codemirror.js:95
CodeMirror.fromTextArea codemirror.js:2114
(anonymous function) codemirror.js:19
f.event.dispatch load-scripts.php:3
f.event.add.h.handle.i