繁体   English   中英

如何将全局变量值从js文件中的函数传递到其他文件中的另一个函数

[英]How to pass a global variable value from a function in a js file to another function in a different file

文件gallery.js基本上创建了一个由用户插入的图像的画廊。

在一个名为gallery.js的文件中,我具有此功能,可以了解用户选择的图像,当他单击该图像时,会在图像周围创建边框。 用户将始终首先使用此功能。

var Nxml=0;
var NameFile =[]; // an array with all images names
var NameXml=[]; // array wth the name of the images selected

gallery.js中的此功能还检索用户使用html输入按钮插入的文件。

function handleFiles() {
"use strict";
var inputElement = document.getElementById("input");
var fileList = inputElement.files;

for(var i = 0; i < fileList.length; i++){

 NameFile.push(fileList[i].name);

 /.../
 }

每次用户单击图库中显示的图像时,都会调用此功能。

function imgClick(img) {

  if (img.className.indexOf('bordered') > -1) {
    img.className = img.className.replace('bordered', '').trim();
      Nxml=Nxml-1;
      alert(Nxml);
  // delete from the "NameXml" array the name of the image clicked

  } else {
    img.className += ' bordered';

   for( var i=0; i<NameFile.length;i++){

   if("imageID"+[i]===img.getAttribute("id")){ 
    Nxml=Nxml+1; 
    NameXml.push(NameFile[i]);

    alert(NameFile[i]);// name of the file selected
    alert(Nxml);// nr of clicks
}
   }
}
}

在另一个名为XML.js的文件中,我有一个函数,该函数会生成一个xml文件,并将其信息发布在表单上,​​并且我希望生成的xml的数量与var“ Nxml”相同,并且每个XML文件的名称都与与所选图像相同。

function download(frm){
    "use strict";
    alert(window.Nxml);
    alert(NameXml);

    for(var j=0; j<Nxml; j++){ 
    var N= NameXml[j];
    var data=fromToXml(frm);
  console.log(data);

  // aqui o nome do ficheiro
  downloadData("text/xml",data, N+".xml");  
    }

    }

你为什么要减少imgClick(img)中的Nxml! 因此,它始终是负数,下载中的for循环不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM