我有以下代码,对于如何将其一直传递给onload函数,我不太清楚。
我试图在不同的地方定义它,但是没有成功,特别是因为它的定义需要在传递给document.on
方法的函数中进行,因为我想获得this
引用的保留权。
$(document).ready(function(e) {
$(document).on('click', "#someId", function(e) {
e.preventDefault();
var variable = $(this).attr('name');
chrome.fileSystem.chooseEntry({
type: 'openFile', accepts:[{
extensions: ['txt']
}]
},
function(fileEntry) {
if (!fileEntry) {
return;
}
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onload = function(e) {
////******* how to access variable here? *******\\\\
chrome.storage.local.set({'txt': e.target.result});
};
reader.readAsArrayBuffer(file);
});
});
});
});
我需要variable
在onload
函数内部访问。我该如何通过?
您可以利用闭包这样做:
onload = function () {
var variable;
$(document).on('click', "#someId", function(e) {
variable = $(this).attr('name');
// use variable here
});
// and here
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句