如何从包含其他脚本的连接脚本中访问变量?

时间:2017-07-30 00:55:51

标签: javascript jquery concatenation nwjs

我有这个代码在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!");’

需要如下(但我需要从(唯一命名的)chooseFile脚本访问“path”而不是硬编码的URL)...

'$("#" +' + 'theEditId).css("background-image", "url(http://www.jqueryscript.net/images/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg)");'

我很确定我没有逃脱逗号或其他内容,这是我正确解决的另一段代码但无法访问“路径”......

'$("#" +' + 'theEditId).css("background-color", "green");'

0 个答案:

没有答案
相关问题