我有这段代码可以在 NW.JS 中打开一个文件。
function chooseFile(name, handleFile) {
var chooser = document.querySelector(name);
chooser.addEventListener("change", function(evt) {
for(var f of this.files){
console.log(f.name);
console.log(f.path);
handleFile(f.name, f.path);
}
}, false);
chooser.click();
}
chooseFile('#fileDialog', function(name, path){ ... /* do something with the file(s) */ });
我已将该代码连接到一个字符串中,以便我可以通过单击按钮将其注入到 div 中(当创建该 div 时),从而生成具有唯一 id 的上述代码。
var letsDoThis = '$(".' + imgUploadClass + '").click(function(){ var leid = this.id; var theEditId = "divId" + leid.slice(5); function ' + imgUploadChoose + '(name, handleFile) { var chooser = document.querySelector(name);chooser.addEventListener("change", function(evt) { for(var f of this.files){console.log(f.name);console.log(f.path);handleFile(f.name, f.path);}}, false);chooser.click();}' + imgUploadChoose + '("#' + imgUploadzz +'", function(name, path){' + 'alert(path);' + ';});});';
测试时,我可以访问警报框中的名称和路径!伟大的!我想在哪里运行的代码
'alert("Hello!");’
需要如下(但不是硬编码的 url,我需要从(唯一命名的)chooseFile 脚本访问“路径”)...
'$("#" +' + 'theEditId).css("background-image", "url(http://www.jqueryscript.net/images/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg)");'
我很确定我没有转义逗号或其他东西,这是我正确触发但无法访问“路径”的另一段代码......
'$("#" +' + 'theEditId).css("background-color", "green");'