gpt4 book ai didi

javascript - 如何将全局变量值从js文件中的函数传递到不同文件中的另一个函数

转载 作者:行者123 更新时间:2023-12-03 01:52:19 25 4
gpt4 key购买 nike

文件 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 循环不起作用。

关于javascript - 如何将全局变量值从js文件中的函数传递到不同文件中的另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50367907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com