gpt4 book ai didi

javascript - 获取变量值并将它们放入另一个文件中

转载 作者:行者123 更新时间:2023-12-02 21:21:13 24 4
gpt4 key购买 nike

我正在对游戏进行 block 编码,我有一个名为 lineCount 的变量,用于计算换行符的数量。然而这个变量位于一个名为 lib-dialog.js 的文件中。当我用innerHTML插入这个变量的值时,我可以通过在soy.js文件(我需要用来处理结果的文件)中创建一个div来获取行的值,但是我需要在变量中使用这个值来放置一个if (行 == 6){ }

// Add the user's code.
if (BlocklyGames.workspace) {
var linesText = document.getElementById('dialogLinesText');
linesText.textContent = '';
// Line produces warning when compiling Puzzle since there is no JavaScript
// generator. But this function is never called in Puzzle, so no matter.
var code = Blockly.JavaScript.workspaceToCode(BlocklyGames.workspace);
code = BlocklyInterface.stripCode(code);
var noComments = code.replace(/\/\/[^\n]*/g, ''); // Inline comments.
noComments = noComments.replace(/\/\*.*\*\//g, ''); /* Block comments. */
noComments = noComments.replace(/[ \t]+\n/g, '\n'); // Trailing spaces.
noComments = noComments.replace(/\n+/g, '\n'); // Blank lines.
noComments = noComments.trim();
var lineCount = noComments.split('\n').length;
var pre = document.getElementById('containerCode');
pre.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = pre.innerHTML;
code = prettyPrintOne(code, 'js');
pre.innerHTML = code;
}
if (lineCount == 1) {
var text = BlocklyGames.getMsg('Games_linesOfCode1');
} else {
var text = BlocklyGames.getMsg('Games_linesOfCode2')
.replace('%1', String(lineCount));
}
linesText.appendChild(document.createTextNode(text));

document.getElementById("contarBloco").innerHTML = lineCount;
var contandoBloco = lineCount;
}

我需要获取变量 lineCount 并将其值放入另一个 js 中。但我只能将其插入到带有 innerHTML 的 div 中

最佳答案

最好使用本地存储。在本地存储中设置localcount的值并到达您想要的地方

var lineCount = noComments.split('\n').length;

localStorage.setItem("lineCount", lineCount); // in first file

var count = localStorage.getItem("lineCount") // in second file

通过此逻辑,您将获得值,但它将是字符串,因此您可以直接使用字符串或使用 parseInt 方法转换为整数

parseInt(count);

也许会有帮助。谢谢

关于javascript - 获取变量值并将它们放入另一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60838006/

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